diff --git a/AUTHORS b/AUTHORS index 044391e4a3..4b1cc7d357 100644 --- a/AUTHORS +++ b/AUTHORS @@ -196,6 +196,7 @@ answer newbie questions, and generally made Django that much better: btoll@bestweb.net C8E Caio Ariede + Caitie Baca Calvin Spealman Cameron Curry Cameron Knight (ckknight) diff --git a/django/test/testcases.py b/django/test/testcases.py index 5f0c819815..c587f770a6 100644 --- a/django/test/testcases.py +++ b/django/test/testcases.py @@ -43,6 +43,7 @@ from django.db.backends.base.base import NO_DB_ALIAS, BaseDatabaseWrapper from django.forms.fields import CharField from django.http import QueryDict from django.http.request import split_domain_port, validate_host +from django.template.base import PartialTemplate from django.test.client import AsyncClient, Client from django.test.html import HTMLParseError, parse_html from django.test.signals import template_rendered @@ -138,10 +139,22 @@ class _AssertTemplateUsedContext: self.rendered_templates.append(template) self.context.append(copy(context)) + @property + def rendered_template_names(self): + return [ + ( + f"{t.origin.template_name}#{t.name}" + if isinstance(t, PartialTemplate) + else t.name + ) + for t in self.rendered_templates + if t.name is not None + ] + def test(self): self.test_case._assert_template_used( self.template_name, - [t.name for t in self.rendered_templates if t.name is not None], + self.rendered_template_names, self.msg_prefix, self.count, ) @@ -159,11 +172,8 @@ 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 rendered_template_names, + self.template_name in self.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/templates/template_used/partials.html b/tests/test_utils/templates/template_used/partials.html new file mode 100644 index 0000000000..028c2d8417 --- /dev/null +++ b/tests/test_utils/templates/template_used/partials.html @@ -0,0 +1,3 @@ +{% partialdef hello %} +

Hello

+{% endpartialdef %} diff --git a/tests/test_utils/tests.py b/tests/test_utils/tests.py index 9c22b61b4f..70cca3d441 100644 --- a/tests/test_utils/tests.py +++ b/tests/test_utils/tests.py @@ -646,6 +646,53 @@ class AssertTemplateUsedContextManagerTests(SimpleTestCase): self.assertTemplateNotUsed(response, "template.html") +@override_settings(ROOT_URLCONF="test_utils.urls") +class AssertTemplateUsedPartialTests(SimpleTestCase): + def test_template_used_pass(self): + with self.assertTemplateUsed("template_used/partials.html#hello"): + render_to_string("template_used/partials.html#hello") + + def test_template_not_used_pass(self): + with self.assertTemplateNotUsed("hello"): + render_to_string("template_used/partials.html#hello") + + def test_template_used_fail(self): + msg = "Template 'hello' was not a template used to render the response." + with ( + self.assertRaisesMessage(AssertionError, msg), + self.assertTemplateUsed("hello"), + ): + render_to_string("template_used/base.html") + + def test_template_not_used_fail(self): + msg = ( + "Template 'template_used/partials.html#hello' was used " + "unexpectedly in rendering the response" + ) + with ( + self.assertRaisesMessage(AssertionError, msg), + self.assertTemplateNotUsed("template_used/partials.html#hello"), + ): + render_to_string("template_used/partials.html#hello") + + def test_template_not_used_pass_non_partial(self): + with self.assertTemplateNotUsed( + "template_used/base.html#template_used/base.html" + ): + render_to_string("template_used/base.html") + + def test_template_used_fail_non_partial(self): + msg = ( + "Template 'template_used/base.html#template_used/base.html' was not a " + "template used to render the response." + ) + with ( + self.assertRaisesMessage(AssertionError, msg), + self.assertTemplateUsed("template_used/base.html#template_used/base.html"), + ): + render_to_string("template_used/base.html") + + class HTMLEqualTests(SimpleTestCase): def test_html_parser(self): element = parse_html("

Hello

")