Amyth
Amyth

Reputation: 32949

check template name in django.test.TestCase

I am trying to write a tests for our simple application. Although, All the tests are running flawlessly, I am still wanting to check the template names that a view is rendering or redirecting to. What would be the best way to check what template does a view render.

Maybe something like:

self.assertTrue('test.html' in self.templates)

or

self.assertTrue(self.template.name, 'test.html')

How can this be acheived.

Upvotes: 3

Views: 2387

Answers (2)

Maxim Cheusov
Maxim Cheusov

Reputation: 128

You should use assertTemplateUsed (docs):

response = self.client.get('/url/')
self.assertTemplateUsed(response, 'test.html')

Upvotes: 4

Steve Bradshaw
Steve Bradshaw

Reputation: 817

From: response = self.client.get("/my/view/url")

you can do

self.assertEqual(response.templates[0].name, "expected_template.html")

or:

self.assertEqual(response.template[0].name, "expected_template.html")

as 'template' and 'templates' are the same array. Subsequent (non-zero) entries of this array list included or extended templates.

Upvotes: 1

Related Questions