#34657: Testing assertions `assertContains` and `assertInHTML` should output the haystack on failure -------------------------------------+------------------------------------- Reporter: Thibaud | Owner: nobody Colas | Type: New | Status: new feature | Component: Testing | Version: framework | Keywords: testcases, unit Severity: Normal | tests, HTML, assertions, testing Triage Stage: | Has patch: 0 Unreviewed | Needs documentation: 0 | Needs tests: 0 Patch needs improvement: 0 | Easy pickings: 0 UI/UX: 0 | -------------------------------------+------------------------------------- If I use the vanilal Python `assertIn` or `assertRegex`, when the assertion fails, it’s very simple to assess what went wrong from the test error only:
{{{ self.assertIn("<b>hey</b>", "<p>Howdy!</p>") AssertionError: '<b>hey</b>' not found in '<p>Howdy!</p>' }}} With Django’s `assertContains` and `assertInHTML`, this gets much harder: {{{ self.assertInHTML("<b>hey</b>", "<p>Howdy!</p>") File "/Users/thibaudcolas/Dev/django/html-testing-with- django/.venv/lib/python3.11/site-packages/django/test/testcases.py", line 1076, in assertInHTML self.assertTrue( AssertionError: False is not true : Couldn't find '<b> hey </b>' in response }}} and: {{{ self.assertContains(res, "<b>hey</b>") File "/Users/thibaudcolas/Dev/django/html-testing-with- django/.venv/lib/python3.11/site-packages/django/test/testcases.py", line 660, in assertContains self.assertTrue( AssertionError: False is not true : Couldn't find '<b>hey</b>' in response }}} --- In both cases, Django doesn’t display the haystack – so I have to waste a lot of time going back to my HTML templates to check what they might be outputting / or potentially load the same scenario in a browser / or manually add `print` statements to my test cases. This is all very time- consuming. Instead, it’d be much better if the haystack was just present. Additionally for `assertInHTML` – it’s annoying that `needle` as displayed in the failure message is the parser’s output, which is therefore broken up over multiple lines. It’d be much nicer if the example above resulted in `AssertionError: False is not true : Couldn't find '<b>hey</b>' in response`. --- Test suite I used to compare output for reference: https://github.com/thibaudcolas/html-testing-with- django/blob/39c82c410cce4bea71ac54be27be67057f4d8dd8/testing_tests/tests.py#L5 -- Ticket URL: <https://code.djangoproject.com/ticket/34657> 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 django-updates+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/01070188c46b6dc6-45772794-a93a-45f3-9a74-6b65395a13a3-000000%40eu-central-1.amazonses.com.