How to assert django uses certain template in pytest

As phd stated in a comment, use the following to assert that a template file is actually used in a view:

response = client.get(article.get_absolute_url())
assert 'article_detail.html' in (t.name for t in response.templates)

Update: Since v3.8.0 (2020-01-14) pytest-django makes all of the assertions in Django's TestCase available in pytest_django.asserts. See Stan Redoute's answer for an example.


To assert whether given template was used to render specific view you can (and even should) use helpers provided by pytest-django:

import pytest
from pytest_django.asserts import assertTemplateUsed

...

def test_should_use_correct_template_to_render_a_view(client):
    response = client.get('.../your-url/')
    assertTemplateUsed(response, 'template_name.html')

pytest-django even uses this exact assertion as an example in documentation.