From e99c7d8847e9006f877ab3cea47f1977652af71f Mon Sep 17 00:00:00 2001 From: Chinmoy Chakraborty Date: Fri, 29 Sep 2023 09:48:45 +0200 Subject: [PATCH] Refs #34657 -- Made assertInHTML() use unparsed needle in error messages. --- AUTHORS | 1 + django/test/testcases.py | 6 +++--- tests/test_utils/tests.py | 7 +++++++ 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/AUTHORS b/AUTHORS index a25695e40d..a633f6e03c 100644 --- a/AUTHORS +++ b/AUTHORS @@ -196,6 +196,7 @@ answer newbie questions, and generally made Django that much better: charly.wilhelm@gmail.com Chason Chaffin Cheng Zhang + Chinmoy Chakraborty Chris Adams Chris Beaven Chris Bennett diff --git a/django/test/testcases.py b/django/test/testcases.py index 6b44c0bd29..60a04bf5d2 100644 --- a/django/test/testcases.py +++ b/django/test/testcases.py @@ -873,13 +873,13 @@ class SimpleTestCase(unittest.TestCase): self.fail(self._formatMessage(msg, standardMsg)) def assertInHTML(self, needle, haystack, count=None, msg_prefix=""): - needle = assert_and_parse_html( + parsed_needle = assert_and_parse_html( self, needle, None, "First argument is not valid HTML:" ) - haystack = assert_and_parse_html( + parsed_haystack = assert_and_parse_html( self, haystack, None, "Second argument is not valid HTML:" ) - real_count = haystack.count(needle) + real_count = parsed_haystack.count(parsed_needle) if count is not None: self.assertEqual( real_count, diff --git a/tests/test_utils/tests.py b/tests/test_utils/tests.py index 8b07d631fb..3ee78a60a3 100644 --- a/tests/test_utils/tests.py +++ b/tests/test_utils/tests.py @@ -983,6 +983,13 @@ class HTMLEqualTests(SimpleTestCase): ) +class InHTMLTests(SimpleTestCase): + def test_needle_msg(self): + msg = "False is not true : Couldn't find 'Hello' in response" + with self.assertRaisesMessage(AssertionError, msg): + self.assertInHTML("Hello", "

Test

") + + class JSONEqualTests(SimpleTestCase): def test_simple_equal(self): json1 = '{"attr1": "foo", "attr2":"baz"}'