Opened 2 years ago
Last modified 2 years ago
#34823 closed Bug
assertTemplateUsed() context manager crashes for templates without names. — at Version 4
| 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 (4)
comment:1 by , 2 years ago
comment:2 by , 2 years ago
| Description: | modified (diff) |
|---|
comment:3 by , 2 years 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 --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 , 2 years ago
| Description: | modified (diff) |
|---|---|
| Needs tests: | unset |
| Patch needs improvement: | unset |
Note:
See TracTickets
for help on using tickets.
Suggested fix: https://github.com/django/django/pull/17237