Opened 10 years ago
Closed 10 years ago
#23300 closed Bug (fixed)
TestCase.assertTemplateUsed passes erroneously on an HttpResponse
Reported by: | Benjamin Zagorsky | Owned by: | Davide Ceretti |
---|---|---|---|
Component: | Testing framework | Version: | dev |
Severity: | Normal | Keywords: | asserttemplateused template |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | yes |
Needs tests: | yes | Patch needs improvement: | yes |
Easy pickings: | no | UI/UX: | no |
Description
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 by , 10 years ago
Summary: | TestCase.assertTemplateUsed passes erroniously on an HttpResponse → TestCase.assertTemplateUsed passes erroneously on an HttpResponse |
---|
comment:2 by , 10 years ago
Needs documentation: | set |
---|---|
Needs tests: | set |
Patch needs improvement: | set |
Triage Stage: | Unreviewed → Accepted |
comment:3 by , 10 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
Version: | 1.6 → master |
comment:4 by , 10 years ago
It's worth noting that response.templates
is a monkey-patch from the Client
; assertTemplateused
won't work in any other context.
comment:5 by , 10 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
Hi,
You can find the documentation for
assertTemplateused
at https://docs.djangoproject.com/en/dev/topics/testing/tools/#django.test.SimpleTestCase.assertTemplateUsed.I looked into the issue a little bit and I agree with your analysis.
As a fix, I would suggest raising an exception when trying to use
assertTemplateUsed
(orassertTemplateNotUsed
) on a response object that doesn't have anytemplates
attributes.Here's a simple patch that should apply cleanly on master:
django/test/testcases.py
With this fix, your example now raises an exception and Django's test suite still passes.