#23300: TestCase.assertTemplateUsed passes erroniously on an HttpResponse
---------------------------------+-----------------------------------------
Reporter: zags | Owner: nobody
Type: Bug | Status: new
Component: Testing | Version: 1.6
framework | Keywords: asserttemplateused template
Severity: Normal | Has patch: 1
Triage Stage: Unreviewed | UI/UX: 0
Easy pickings: 0 |
---------------------------------+-----------------------------------------
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
--
Ticket URL: <https://code.djangoproject.com/ticket/23300>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/047.3be6722d1df29a2592dce740eb6f76ff%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.