{{{
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.
* needs_better_patch: => 0
* needs_docs: => 0
* needs_tests: => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/23300#comment:1>
* needs_better_patch: 0 => 1
* stage: Unreviewed => Accepted
* needs_tests: 0 => 1
* needs_docs: 0 => 1
Comment:
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` (or `assertTemplateNotUsed`) on a response object
that doesn't have any `templates` attributes.
Here's a simple patch that should apply cleanly on master:
{{{#!diff
diff --git a/django/test/testcases.py b/django/test/testcases.py
index d46f672..6f7ab71 100644
--- a/django/test/testcases.py
+++ b/django/test/testcases.py
@@ -502,10 +502,12 @@ class SimpleTestCase(unittest.TestCase):
"the response" % formset)
def _assert_template_used(self, response, template_name, msg_prefix):
-
if response is None and template_name is None:
raise TypeError('response and/or template_name argument must
be provided')
+ if template_name is not None and response is not None and not
hasattr(response, 'templates'):
+ raise ValueError("Response object %r has no attribute
'templates'." % response)
+
if msg_prefix:
msg_prefix += ": "
}}}
With this fix, your example now raises an exception and Django's test
suite still passes.
--
Ticket URL: <https://code.djangoproject.com/ticket/23300#comment:2>
* owner: nobody => davide-ceretti
* status: new => assigned
* version: 1.6 => master
--
Ticket URL: <https://code.djangoproject.com/ticket/23300#comment:3>
Comment (by loic):
It's worth noting that `response.templates` is a monkey-patch from the
`Client`; `assertTemplateused` won't work in any other context.
--
Ticket URL: <https://code.djangoproject.com/ticket/23300#comment:4>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"2d06e3155a13e3ca9e63b97c7c9499a1e1ffd654"]:
{{{
#!CommitTicketReference repository=""
revision="2d06e3155a13e3ca9e63b97c7c9499a1e1ffd654"
Fixed #23300 -- Made assertTemplateUsed throw an error on responses not
fetched using the test client.
Thanks zags for the report and bmispelon for the patch.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/23300#comment:5>