summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCaitie Baca <caitie.baca@powereng.com>2025-09-11 13:06:47 -0700
committerJacob Walls <jacobtylerwalls@gmail.com>2025-09-15 16:45:13 -0400
commit0e0b4214c350da9b627a67987b13ec334e1de033 (patch)
tree84e10f95c8df52de45f8e113550f7451af4c1f8b
parent6e89271a8507fe272d11814975500a1b40303a04 (diff)
Fixed #36589 -- Made assertTemplateUsed/NotUsed track full path for PartialTemplate.
Previously, assertTemplateUsed only matched partial names, ignoring the template origin. This caused assertions on partials specified by origin ("template.html#partial") to fail. Refs #36410.
-rw-r--r--AUTHORS1
-rw-r--r--django/test/testcases.py20
-rw-r--r--tests/test_utils/templates/template_used/partials.html3
-rw-r--r--tests/test_utils/tests.py47
4 files changed, 66 insertions, 5 deletions
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 <caio.ariede@gmail.com>
+ Caitie Baca <caitlin.baca@yahoo.com>
Calvin Spealman <ironfroggy@gmail.com>
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 %}
+<p>Hello</p>
+{% 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("<div><p>Hello</p></div>")