diff options
| author | Arian <arian.kulmer@web.de> | 2023-09-09 01:28:36 +0200 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2023-09-22 10:51:10 +0200 |
| commit | 51d703a27fee518491adee1e07e2b857a90b2c8d (patch) | |
| tree | 668dc305027b0e119cb33b55ff4254b4a411ac9a | |
| parent | 4de31ec680df062e5964b630f1b881ead5004e15 (diff) | |
Fixed #34823 -- Fixed assertTemplateUsed() context manager crash on unnamed templates.
| -rw-r--r-- | django/test/testcases.py | 9 | ||||
| -rw-r--r-- | tests/test_utils/tests.py | 15 |
2 files changed, 17 insertions, 7 deletions
diff --git a/django/test/testcases.py b/django/test/testcases.py index 8855c7adc5..6b44c0bd29 100644 --- a/django/test/testcases.py +++ b/django/test/testcases.py @@ -114,18 +114,16 @@ class _AssertTemplateUsedContext: self.count = count self.rendered_templates = [] - self.rendered_template_names = [] self.context = ContextList() def on_template_render(self, sender, signal, template, context, **kwargs): self.rendered_templates.append(template) - self.rendered_template_names.append(template.name) self.context.append(copy(context)) def test(self): self.test_case._assert_template_used( self.template_name, - self.rendered_template_names, + [t.name for t in self.rendered_templates if t.name is not None], self.msg_prefix, self.count, ) @@ -143,8 +141,11 @@ class _AssertTemplateUsedContext: class _AssertTemplateNotUsedContext(_AssertTemplateUsedContext): def test(self): + rendered_template_names = [ + t.name for t in self.rendered_templates if t.name is not None + ] self.test_case.assertFalse( - self.template_name in self.rendered_template_names, + self.template_name in rendered_template_names, f"{self.msg_prefix}Template '{self.template_name}' was used " f"unexpectedly in rendering the response", ) diff --git a/tests/test_utils/tests.py b/tests/test_utils/tests.py index f05b1f1e71..8b07d631fb 100644 --- a/tests/test_utils/tests.py +++ b/tests/test_utils/tests.py @@ -26,6 +26,7 @@ from django.forms import ( formset_factory, ) from django.http import HttpResponse +from django.template import Context, Template from django.template.loader import render_to_string from django.test import ( SimpleTestCase, @@ -530,12 +531,20 @@ class AssertTemplateUsedContextManagerTests(SimpleTestCase): with self.assertTemplateUsed("template_used/base.html"): render_to_string("template_used/alternative.html") - with self.assertRaisesMessage( - AssertionError, "No templates used to render the response" - ): + msg = "No templates used to render the response" + with self.assertRaisesMessage(AssertionError, msg): response = self.client.get("/test_utils/no_template_used/") self.assertTemplateUsed(response, "template_used/base.html") + with self.assertRaisesMessage(AssertionError, msg): + with self.assertTemplateUsed("template_used/base.html"): + self.client.get("/test_utils/no_template_used/") + + with self.assertRaisesMessage(AssertionError, msg): + with self.assertTemplateUsed("template_used/base.html"): + template = Template("template_used/alternative.html", name=None) + template.render(Context()) + def test_msg_prefix(self): msg_prefix = "Prefix" msg = f"{msg_prefix}: No templates used to render the response" |
