1
0
mirror of https://github.com/django/django.git synced 2025-08-21 01:09:13 +00:00

Split monolithic AssertContainsTests.test_contains() into focused tests.

Added a local helper to test assertion messages with and without a given
msg_prefix.
This commit is contained in:
Clifford Gama 2025-06-24 21:33:37 +02:00 committed by nessita
parent 6320915053
commit 024ea0f783

View File

@ -35,167 +35,179 @@ class TestDataMixin:
) )
class ExtraAssertMixin:
def assertRaisesPrefixedMessage(
self,
method,
*method_args,
expected_msg,
msg_prefix="abc",
**method_kwargs,
):
"""
`method` raises an AssertionError with and without a prefixed message.
:param method: The assertion method to test.
:param method_args: Positional arguments to pass to the method.
:param expected_msg: The expected base error message (required keyword-only).
:param msg_prefix: Optional prefix to be added to the message in the second
subTest.
:param method_kwargs: Keyword arguments to pass to the method.
Used internally for testing Django's assertions.
"""
with (
self.subTest("without prefix"),
self.assertRaisesMessage(AssertionError, expected_msg),
):
method(*method_args, **method_kwargs)
with (
self.subTest("with prefix"),
self.assertRaisesMessage(AssertionError, f"{msg_prefix}: {expected_msg}"),
):
method(*method_args, **method_kwargs, msg_prefix=msg_prefix)
@override_settings(ROOT_URLCONF="test_client_regress.urls") @override_settings(ROOT_URLCONF="test_client_regress.urls")
class AssertContainsTests(SimpleTestCase): class AssertContainsTests(ExtraAssertMixin, SimpleTestCase):
def test_contains(self):
"Responses can be inspected for content, including counting repeated substrings" def test_basic_contains_not_contains(self):
response = self.client.get("/no_template_view/") response = self.client.get("/no_template_view/")
self.assertNotContains(response, "never") with self.subTest("assertNotContains"):
self.assertContains(response, "never", 0) self.assertNotContains(response, "never")
self.assertContains(response, "once")
self.assertContains(response, "once", 1)
self.assertContains(response, "twice")
self.assertContains(response, "twice", 2)
try: cases = [
self.assertContains(response, "text", status_code=999) ("never", 0),
except AssertionError as e: ("once", None),
self.assertIn( ("once", 1),
"Couldn't retrieve content: Response code was 200 (expected 999)", ("twice", None),
str(e), ("twice", 2),
) ]
try:
self.assertContains(response, "text", status_code=999, msg_prefix="abc")
except AssertionError as e:
self.assertIn(
"abc: Couldn't retrieve content: Response code was 200 (expected 999)",
str(e),
)
try: for text, expected_count in cases:
self.assertNotContains(response, "text", status_code=999) with self.subTest(text=text, expected_count=expected_count):
except AssertionError as e: if expected_count is not None:
self.assertIn( self.assertContains(response, text, count=expected_count)
"Couldn't retrieve content: Response code was 200 (expected 999)", else:
str(e), self.assertContains(response, text)
)
try:
self.assertNotContains(response, "text", status_code=999, msg_prefix="abc")
except AssertionError as e:
self.assertIn(
"abc: Couldn't retrieve content: Response code was 200 (expected 999)",
str(e),
)
try: def test_contains_with_wrong_status_code(self):
self.assertNotContains(response, "once") response = self.client.get("/no_template_view/")
except AssertionError as e: msg = "Couldn't retrieve content: Response code was 200 (expected 999)"
self.assertIn( self.assertRaisesPrefixedMessage(
"'once' unexpectedly found in the following response\n" self.assertContains,
f"{response.content}", response,
str(e), "text",
) status_code=999,
try: expected_msg=msg,
self.assertNotContains(response, "once", msg_prefix="abc") )
except AssertionError as e:
self.assertIn(
"abc: 'once' unexpectedly found in the following response\n"
f"{response.content}",
str(e),
)
try: def test_not_contains_with_wrong_status_code(self):
self.assertContains(response, "never", 1) response = self.client.get("/no_template_view/")
except AssertionError as e: msg = "Couldn't retrieve content: Response code was 200 (expected 999)"
self.assertIn( self.assertRaisesPrefixedMessage(
"Found 0 instances of 'never' (expected 1) in the following response\n" self.assertNotContains,
f"{response.content}", response,
str(e), "text",
) status_code=999,
try: expected_msg=msg,
self.assertContains(response, "never", 1, msg_prefix="abc") )
except AssertionError as e:
self.assertIn(
"abc: Found 0 instances of 'never' (expected 1) in the following "
f"response\n{response.content}",
str(e),
)
try: def test_not_contains_failure(self):
self.assertContains(response, "once", 0) response = self.client.get("/no_template_view/")
except AssertionError as e: msg = f"'once' unexpectedly found in the following response\n{response.content}"
self.assertIn( self.assertRaisesPrefixedMessage(
"Found 1 instances of 'once' (expected 0) in the following response\n" self.assertNotContains,
f"{response.content}", response,
str(e), "once",
) expected_msg=msg,
try: )
self.assertContains(response, "once", 0, msg_prefix="abc")
except AssertionError as e:
self.assertIn(
"abc: Found 1 instances of 'once' (expected 0) in the following "
f"response\n{response.content}",
str(e),
)
try: def test_count_mismatch(self):
self.assertContains(response, "once", 2) response = self.client.get("/no_template_view/")
except AssertionError as e: msg = (
self.assertIn( "Found 0 instances of 'never' (expected 1) in the following response\n"
"Found 1 instances of 'once' (expected 2) in the following response\n" f"{response.content}"
f"{response.content}", )
str(e), self.assertRaisesPrefixedMessage(
) self.assertContains,
try: response,
self.assertContains(response, "once", 2, msg_prefix="abc") "never",
except AssertionError as e: count=1,
self.assertIn( expected_msg=msg,
"abc: Found 1 instances of 'once' (expected 2) in the following " )
f"response\n{response.content}",
str(e),
)
try: def test_unexpected_presence(self):
self.assertContains(response, "twice", 1) response = self.client.get("/no_template_view/")
except AssertionError as e: msg = (
self.assertIn( "Found 1 instances of 'once' (expected 0) in the following "
"Found 2 instances of 'twice' (expected 1) in the following response\n" f"response\n{response.content}"
f"{response.content}", )
str(e), self.assertRaisesPrefixedMessage(
) self.assertContains,
try: response,
self.assertContains(response, "twice", 1, msg_prefix="abc") "once",
except AssertionError as e: count=0,
self.assertIn( expected_msg=msg,
"abc: Found 2 instances of 'twice' (expected 1) in the following " )
f"response\n{response.content}",
str(e),
)
try: def test_insufficient_count(self):
self.assertContains(response, "thrice") response = self.client.get("/no_template_view/")
except AssertionError as e: msg = (
self.assertIn( "Found 1 instances of 'once' (expected 2) in the following response\n"
f"Couldn't find 'thrice' in the following response\n{response.content}", f"{response.content}"
str(e), )
) self.assertRaisesPrefixedMessage(
try: self.assertContains,
self.assertContains(response, "thrice", msg_prefix="abc") response,
except AssertionError as e: "once",
self.assertIn( 2,
"abc: Couldn't find 'thrice' in the following response\n" expected_msg=msg,
f"{response.content}", )
str(e),
)
try: def test_excessive_count(self):
self.assertContains(response, "thrice", 3) response = self.client.get("/no_template_view/")
except AssertionError as e: msg = (
self.assertIn( "Found 2 instances of 'twice' (expected 1) in the following response\n"
"Found 0 instances of 'thrice' (expected 3) in the following response\n" f"{response.content}"
f"{response.content}", )
str(e), self.assertRaisesPrefixedMessage(
) self.assertContains,
try: response,
self.assertContains(response, "thrice", 3, msg_prefix="abc") "twice",
except AssertionError as e: count=1,
self.assertIn( expected_msg=msg,
"abc: Found 0 instances of 'thrice' (expected 3) in the following " )
f"response\n{response.content}",
str(e),
)
def test_missing_content(self):
response = self.client.get("/no_template_view/")
msg = f"Couldn't find 'thrice' in the following response\n{response.content}"
self.assertRaisesPrefixedMessage(
self.assertContains,
response,
"thrice",
expected_msg=msg,
msg_prefix="Custom prexix",
)
def test_missing_content_with_count(self):
response = self.client.get("/no_template_view/")
msg = (
"Found 0 instances of 'thrice' (expected 3) in the following "
f"response\n{response.content}"
)
self.assertRaisesPrefixedMessage(
self.assertContains,
response,
"thrice",
3,
expected_msg=msg,
)
def test_long_content(self):
long_content = ( long_content = (
b"This is a very very very very very very very very long message which " b"This is a very very very very very very very very long message which "
b"exceeds the max limit of truncation." b"exceeds the max limit of truncation."