summaryrefslogtreecommitdiff
path: root/tests/test_client_regress
diff options
context:
space:
mode:
authorSkyiesac <jainsachi1202@gmail.com>2025-11-17 00:46:37 +0530
committerJacob Walls <jacobtylerwalls@gmail.com>2025-12-03 09:18:10 -0500
commitd338c2243fa0786225e056c5d7003f7ad67d44de (patch)
tree9e1af2794830ffc04f8936027fb7e1ce747dd9c0 /tests/test_client_regress
parentdba622ebc1f6a6700b75303a65f8a334bd46bd8e (diff)
Fixed #36280 -- Replaced exception checks with assertRaisesMessage().
Diffstat (limited to 'tests/test_client_regress')
-rw-r--r--tests/test_client_regress/tests.py134
1 files changed, 52 insertions, 82 deletions
diff --git a/tests/test_client_regress/tests.py b/tests/test_client_regress/tests.py
index 7168c8b078..e3754ef904 100644
--- a/tests/test_client_regress/tests.py
+++ b/tests/test_client_regress/tests.py
@@ -309,15 +309,13 @@ class AssertTemplateUsedTests(TestDataMixin, TestCase):
# The no template case doesn't mess with the template assertions
self.assertTemplateNotUsed(response, "GET Template")
- try:
+ msg = "No templates used to render the response"
+ with self.assertRaisesMessage(AssertionError, msg):
self.assertTemplateUsed(response, "GET Template")
- except AssertionError as e:
- self.assertIn("No templates used to render the response", str(e))
- try:
+ msg = "No templates used to render the response"
+ with self.assertRaisesMessage(AssertionError, msg):
self.assertTemplateUsed(response, "GET Template", msg_prefix="abc")
- except AssertionError as e:
- self.assertIn("abc: No templates used to render the response", str(e))
msg = "No templates used to render the response"
with self.assertRaisesMessage(AssertionError, msg):
@@ -400,23 +398,19 @@ class AssertRedirectsTests(ExtraAssertMixin, SimpleTestCase):
"""
# This page will redirect with code 301, not 302
response = self.client.get("/permanent_redirect_view/")
- try:
+ msg = (
+ "Response didn't redirect as expected: Response code was 301 "
+ "(expected 302)"
+ )
+ with self.assertRaisesMessage(AssertionError, msg):
self.assertRedirects(response, "/get_view/")
- except AssertionError as e:
- self.assertIn(
- "Response didn't redirect as expected: Response code was 301 "
- "(expected 302)",
- str(e),
- )
- try:
+ msg = (
+ "abc: Response didn't redirect as expected: Response code was 301 "
+ "(expected 302)"
+ )
+ with self.assertRaisesMessage(AssertionError, msg):
self.assertRedirects(response, "/get_view/", msg_prefix="abc")
- except AssertionError as e:
- self.assertIn(
- "abc: Response didn't redirect as expected: Response code was 301 "
- "(expected 302)",
- str(e),
- )
def test_followed_redirect_unexpected_initial_status_code(self):
response = self.client.get("/permanent_redirect_view/", follow=True)
@@ -452,35 +446,25 @@ class AssertRedirectsTests(ExtraAssertMixin, SimpleTestCase):
parameters.
"""
response = self.client.get("/redirect_view/", {"var": "value"})
- try:
+ msg = "Response redirected to '/get_view/?var=value', expected '/get_view/'"
+ with self.assertRaisesMessage(AssertionError, msg):
self.assertRedirects(response, "/get_view/")
- except AssertionError as e:
- self.assertIn(
- "Response redirected to '/get_view/?var=value', expected '/get_view/'",
- str(e),
- )
- try:
+ msg = (
+ "abc: Response redirected to '/get_view/?var=value', expected '/get_view/'"
+ )
+ with self.assertRaisesMessage(AssertionError, msg):
self.assertRedirects(response, "/get_view/", msg_prefix="abc")
- except AssertionError as e:
- self.assertIn(
- "abc: Response redirected to '/get_view/?var=value', expected "
- "'/get_view/'",
- str(e),
- )
def test_incorrect_target(self):
"An assertion is raised if the response redirects to another target"
response = self.client.get("/permanent_redirect_view/")
- try:
+ msg = (
+ "Response didn't redirect as expected: Response code was 301 (expected 302)"
+ )
+ with self.assertRaisesMessage(AssertionError, msg):
# Should redirect to get_view
self.assertRedirects(response, "/some_view/")
- except AssertionError as e:
- self.assertIn(
- "Response didn't redirect as expected: Response code was 301 "
- "(expected 302)",
- str(e),
- )
def test_target_page(self):
"""
@@ -488,27 +472,23 @@ class AssertRedirectsTests(ExtraAssertMixin, SimpleTestCase):
retrieved as expected.
"""
response = self.client.get("/double_redirect_view/")
- try:
+ msg = (
+ "Couldn't retrieve redirection page '/permanent_redirect_view/': "
+ "response code was 301 (expected 200)"
+ )
+ with self.assertRaisesMessage(AssertionError, msg):
# The redirect target responds with a 301 code, not 200
self.assertRedirects(response, "http://testserver/permanent_redirect_view/")
- except AssertionError as e:
- self.assertIn(
- "Couldn't retrieve redirection page '/permanent_redirect_view/': "
- "response code was 301 (expected 200)",
- str(e),
- )
- try:
+ msg = (
+ "abc: Couldn't retrieve redirection page '/permanent_redirect_view/': "
+ "response code was 301 (expected 200)"
+ )
+ with self.assertRaisesMessage(AssertionError, msg):
# The redirect target responds with a 301 code, not 200
self.assertRedirects(
response, "http://testserver/permanent_redirect_view/", msg_prefix="abc"
)
- except AssertionError as e:
- self.assertIn(
- "abc: Couldn't retrieve redirection page '/permanent_redirect_view/': "
- "response code was 301 (expected 200)",
- str(e),
- )
def test_redirect_chain(self):
"You can follow a redirect chain of multiple redirects"
@@ -632,23 +612,18 @@ class AssertRedirectsTests(ExtraAssertMixin, SimpleTestCase):
"""
# This page will redirect with code 301, not 302
response = self.client.get("/get_view/", follow=True)
- try:
+ msg = (
+ "Response didn't redirect as expected: Response code was 200 (expected 302)"
+ )
+ with self.assertRaisesMessage(AssertionError, msg):
self.assertRedirects(response, "/get_view/")
- except AssertionError as e:
- self.assertIn(
- "Response didn't redirect as expected: Response code was 200 "
- "(expected 302)",
- str(e),
- )
- try:
+ msg = (
+ "abc: Response didn't redirect as expected: Response code was 200 "
+ "(expected 302)"
+ )
+ with self.assertRaisesMessage(AssertionError, msg):
self.assertRedirects(response, "/get_view/", msg_prefix="abc")
- except AssertionError as e:
- self.assertIn(
- "abc: Response didn't redirect as expected: Response code was 200 "
- "(expected 302)",
- str(e),
- )
def test_redirect_on_non_redirect_page(self):
"""
@@ -657,23 +632,18 @@ class AssertRedirectsTests(ExtraAssertMixin, SimpleTestCase):
"""
# This page will redirect with code 301, not 302
response = self.client.get("/get_view/")
- try:
+ msg = (
+ "Response didn't redirect as expected: Response code was 200 (expected 302)"
+ )
+ with self.assertRaisesMessage(AssertionError, msg):
self.assertRedirects(response, "/get_view/")
- except AssertionError as e:
- self.assertIn(
- "Response didn't redirect as expected: Response code was 200 "
- "(expected 302)",
- str(e),
- )
- try:
+ msg = (
+ "abc: Response didn't redirect as expected: Response code was 200 "
+ "(expected 302)"
+ )
+ with self.assertRaisesMessage(AssertionError, msg):
self.assertRedirects(response, "/get_view/", msg_prefix="abc")
- except AssertionError as e:
- self.assertIn(
- "abc: Response didn't redirect as expected: Response code was 200 "
- "(expected 302)",
- str(e),
- )
def test_redirect_scheme(self):
"""