TestCase.assertTemplateUsed passes erroneously on an HttpResponse
|Reported by:||zags||Owned by:||davide-ceretti|
|Has patch:||yes||Needs documentation:||yes|
|Needs tests:||yes||Patch needs improvement:||yes|
The following test should fail. It is asserting that the response both renders and does not render the template "xxx.html", which is a contradiction. However, this test passes.
from django.test import TestCase from django.http.response import HttpResponse class TestTemplateUsed(TestCase): def test_template_used(self): response = HttpResponse("xxx") self.assertTemplateUsed(response, "xxx.html") self.assertTemplateNotUsed(response, "xxx.html")
The issue is that HttpResponse objects have no "template" attribute, which assertTemplateUsed interprets as an intention to use a context manager rather than an assertion.
def assertTemplateUsed(self, response=None, template_name=None, msg_prefix=''): #... if not hasattr(response, 'templates') or (response is None and template_name): #...
I'm not sure why this is implemented this way, but from the docs (I can only find the 1.5 docs of this feature; I'm not sure where the relevant docs are for 1.6 and later), it seems far more sensible to implement this as:
def assertTemplateUsed(self, response=None, template_name=None, msg_prefix=''): #... if (response is None and template_name): #...
Just to call out an important point, unless I am missing something, this feature is entirely undocumented in v1.6 and v1.7
Change History (5)
comment:1 Changed 7 months ago by zags
- Needs documentation unset
- Needs tests unset
- Patch needs improvement unset
- Summary changed from TestCase.assertTemplateUsed passes erroniously on an HttpResponse to TestCase.assertTemplateUsed passes erroneously on an HttpResponse
comment:2 Changed 6 months ago by bmispelon
- Needs documentation set
- Needs tests set
- Patch needs improvement set
- Triage Stage changed from Unreviewed to Accepted
comment:3 Changed 5 months ago by davide-ceretti
- Owner changed from nobody to davide-ceretti
- Status changed from new to assigned
- Version changed from 1.6 to master