#35481: assertTemplateUsed false positive
---------------------------------------------+------------------------
Reporter: Shiva Kumar | Owner: nobody
Type: Bug | Status: new
Component: Testing framework | Version: dev
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
---------------------------------------------+------------------------
assertTemplateUsed can be used
[https://docs.djangoproject.com/en/dev/topics/testing/tools/#django.test.SimpleTestCase.assertTemplateUsed
both as a function and as a context manager].
When used as a function, both `request` and `template_name` should be
passed. When used as a context manager, only `template_name` needs to be
passed.
Consider a test case,
{{{
def test_template_used(self):
resp = self.client.get("/home/")
self.assertEqual(resp.status_code, 200)
self.assertTemplateUsed("home.html")
}}}
In this case, the last statement returns a context manager object,
resulting in no assertion checks, and the test case passes. The test case
passed even if the wrong `template_name` is provided.
This scenario arises solely because `assertTemplateUsed` assumes that
[https://github.com/django/django/blob/main/django/test/testcases.py#L750-L751
when only `template_name` is provided, it must have been used as a context
manager].
Had there been a keyword-only argument restriction, this scenario wouldn't
have arisen.
This bug is applicable to `assertTemplateNotUsed` as well.
If the bug is deemed valid, please assign the ticket to me.
--
Ticket URL: <https://code.djangoproject.com/ticket/35481>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/0107018fb65c8150-03b74f56-de06-431f-9606-c12540ec5025-000000%40eu-central-1.amazonses.com.