Opened 3 hours ago
Last modified 3 hours ago
#36859 assigned New feature
SimpleTestCase.assertContains can't be called multiple times on a streaming response
| Reported by: | Baptiste Mispelon | Owned by: | Baptiste Mispelon |
|---|---|---|---|
| Component: | Testing framework | Version: | 6.0 |
| Severity: | Normal | Keywords: | assertContains, streaming |
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
I recently got an unexpected test failure when using SimpleTestCase.assertContains():
def test_some_view(self): response = self.client.get("/some/url/") self.assertContains(response, "something") self.assertContains(response, "another thing") # This failed
Even though my view at /some/url/ was returning a response that did include both "something" and "another thing", the test was failing on the second assertion. After some confusion, I eventually figured out that the cause of this failure was that the view was returning a StreamingHttpResponse.
This failure makes sense if you understand the internals of streaming responses, but I'd argue that the test code shouldn't have to care about the nature of the response, and that repeated calls to assertContains should work transparently.
As far as I can tell, this behavior is not documented (one way or another) and has always been present (see 82b3e6ffcb9d810cc0e3ec27d25f89ce1fd525e0).
(PR with test case and tentative fix will be attached to this ticket soon)
PR here: https://github.com/django/django/pull/20527