Opened 16 months ago

Closed 15 months ago

Last modified 15 months ago

#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 Arian)

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

PR

Change History (7)

comment:2 by Arian, 16 months ago

Description: modified (diff)

comment:3 by Mariusz Felisiak, 16 months ago

Has patch: set
Needs tests: set
Owner: changed from nobody to Arian
Patch needs improvement: set
Status: newassigned
Summary: _assert_template_used fails with anonymous templatesassertTemplateUsed() context manager crashes for templates without names.
Triage Stage: UnreviewedAccepted

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"
Last edited 16 months ago by Mariusz Felisiak (previous) (diff)

comment:4 by Arian, 15 months ago

Description: modified (diff)
Needs tests: unset
Patch needs improvement: unset

comment:5 by Mariusz Felisiak, 15 months ago

Triage Stage: AcceptedReady for checkin

comment:6 by Mariusz Felisiak <felisiak.mariusz@…>, 15 months ago

Resolution: fixed
Status: assignedclosed

In 51d703a2:

Fixed #34823 -- Fixed assertTemplateUsed() context manager crash on unnamed templates.

comment:7 by Mariusz Felisiak <felisiak.mariusz@…>, 15 months ago

In 7683c86:

[5.0.x] Fixed #34823 -- Fixed assertTemplateUsed() context manager crash on unnamed templates.

Backport of 51d703a27fee518491adee1e07e2b857a90b2c8d from main

Note: See TracTickets for help on using tickets.
Back to Top