#34823 closed Bug (fixed)
assertTemplateUsed() context manager crashes for templates without names.
Reported by: | Arian | Owned by: | Arian |
---|---|---|---|
Component: | Testing framework | Version: | 4.2 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Ready for checkin | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
When template_names includes at least one None type the string join will fail.
This occurs when a rendered template does not define a name.
Traceback (most recent call last): File "/home/.../admin-frontend/admin_frontend/admin_cash/tests/test_views.py", line 19, in test_get_context_data with self.assertTemplateUsed('admin_cash/bill_validator_logs.html'): File "/home/../3.10/lib/python3.10/site-packages/django/test/testcases.py", line 146, in __exit__ self.test() File "/home/../3.10/lib/python3.10/site-packages/django/test/testcases.py", line 131, in test self.test_case._assert_template_used( File "/home/../3.10/lib/python3.10/site-packages/django/test/testcases.py", line 828, in _assert_template_used % (template_name, ", ".join(template_names)), TypeError: sequence item 0: expected str instance, NoneType found
Change History (7)
comment:1 by , 16 months ago
comment:2 by , 16 months ago
Description: | modified (diff) |
---|
comment:3 by , 16 months ago
Has patch: | set |
---|---|
Needs tests: | set |
Owner: | changed from | to
Patch needs improvement: | set |
Status: | new → assigned |
Summary: | _assert_template_used fails with anonymous templates → assertTemplateUsed() context manager crashes for templates without names. |
Triage Stage: | Unreviewed → Accepted |
Thanks for the report, I was able to reproduce it with the following test:
{{{!diff
diff --git a/tests/test_utils/tests.py b/tests/test_utils/tests.py
index cdb0453e44..b0eb0b0f83 100644
--- a/tests/test_utils/tests.py
+++ b/tests/test_utils/tests.py
@@ -547,6 +547,12 @@ class AssertTemplateUsedContextManagerTests(SimpleTestCase):
response = self.client.get("/test_utils/no_template_used/")
self.assertTemplateUsed(response, "template_used/base.html")
+ with self.assertRaisesMessage(
+ AssertionError, "No templates used to render the response"
+ ):
+ with self.assertTemplateUsed("template_used/base.html"):
+ self.client.get("/test_utils/no_template_used/")
+
def test_msg_prefix(self):
msg_prefix = "Prefix"
msg = f"{msg_prefix}: No templates used to render the response"
}}}
comment:4 by , 15 months ago
Description: | modified (diff) |
---|---|
Needs tests: | unset |
Patch needs improvement: | unset |
comment:5 by , 15 months ago
Triage Stage: | Accepted → Ready for checkin |
---|
Suggested fix: https://github.com/django/django/pull/17237