3

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.

2 Answers 2

4

You should use assertTemplateUsed (docs):

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

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.