How to assert django uses particular template in pytest
Asked Answered
A

3

20

In the unittest style, I can test that a page uses a particular template by calling assertTemplateUsed. This is useful, for example, when Django inserts values through a template, so that I can't just test string equality.

How should I write the equivalent statement in pytest?

I've been looking in pytest-django but don't see how to do it.

Albertoalberts answered 28/8, 2017 at 20:2 Comment(4)
I dont understand your question you want to test that the data that you pass to a template is correctly rendered?Cardsharp
@Augustin No, I want to test that when I get a page, it's using the template file I expect it to use.Albertoalberts
If you get the "Page" then you are getting the template, if not, what do you mean by getting a "page"? Could you provide some sort of code so I can help you out?Cardsharp
The code for assertTemplateUsed and _assert_template_used show there is an attribute .templates in reponse. So you need something like assert response.templates and ('mytemplate' in [t.name for t in response.templates if t.name is not None]).Jordison
O
19

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.

Oxheart answered 20/6, 2018 at 9:21 Comment(3)
This a workaround rather than actual solution to the problem.Stewpan
Why do you say that? It's what Django does in assertTemplateUsed(). Wrap it in a function and that's it. What more do you want for it to not be a "workaround"?Oxheart
Maybe “workaround” was an overstatement, however there’s a dedicated assertion provided by pytest_django.Stewpan
S
20

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.

Stewpan answered 24/6, 2020 at 9:19 Comment(0)
O
19

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.

Oxheart answered 20/6, 2018 at 9:21 Comment(3)
This a workaround rather than actual solution to the problem.Stewpan
Why do you say that? It's what Django does in assertTemplateUsed(). Wrap it in a function and that's it. What more do you want for it to not be a "workaround"?Oxheart
Maybe “workaround” was an overstatement, however there’s a dedicated assertion provided by pytest_django.Stewpan
C
0

If I understand clearly, you want to test if Django renders the data that you pass to the template correctly. If this is the case, then the concepts are wrong, you should test the data gathered in your view first, and then make sure it calls the template. Testing that the template contains the correct data would be testing Django framework itself.

Cardsharp answered 28/8, 2017 at 20:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.