Skip to content

Commit

Permalink
[3.13] gh-120544: Add else: fail() to tests where exception is expe…
Browse files Browse the repository at this point in the history
…cted (GH-120545) (#120546)

gh-120544: Add `else: fail()` to tests where exception is expected (GH-120545)
(cherry picked from commit 42ebdd8)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
  • Loading branch information
miss-islington and sobolevn authored Jun 15, 2024
1 parent e54a28b commit 767c955
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Lib/test/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1836,6 +1836,8 @@ def f():
except self.failureException:
with support.captured_stderr() as err:
sys.__excepthook__(*sys.exc_info())
else:
self.fail("assertRaisesRegex should have failed.")

self.assertIn("aab", err.getvalue())

Expand Down
10 changes: 10 additions & 0 deletions Lib/test/test_unittest/test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -1132,6 +1132,8 @@ def testAssertMultiLineEqual(self):
# need to remove the first line of the error message
error = str(e).split('\n', 1)[1]
self.assertEqual(sample_text_error, error)
else:
self.fail(f'{self.failureException} not raised')

def testAssertEqualSingleLine(self):
sample_text = "laden swallows fly slowly"
Expand All @@ -1148,6 +1150,8 @@ def testAssertEqualSingleLine(self):
# need to remove the first line of the error message
error = str(e).split('\n', 1)[1]
self.assertEqual(sample_text_error, error)
else:
self.fail(f'{self.failureException} not raised')

def testAssertEqualwithEmptyString(self):
'''Verify when there is an empty string involved, the diff output
Expand All @@ -1165,6 +1169,8 @@ def testAssertEqualwithEmptyString(self):
# need to remove the first line of the error message
error = str(e).split('\n', 1)[1]
self.assertEqual(sample_text_error, error)
else:
self.fail(f'{self.failureException} not raised')

def testAssertEqualMultipleLinesMissingNewlineTerminator(self):
'''Verifying format of diff output from assertEqual involving strings
Expand All @@ -1185,6 +1191,8 @@ def testAssertEqualMultipleLinesMissingNewlineTerminator(self):
# need to remove the first line of the error message
error = str(e).split('\n', 1)[1]
self.assertEqual(sample_text_error, error)
else:
self.fail(f'{self.failureException} not raised')

def testAssertEqualMultipleLinesMismatchedNewlinesTerminators(self):
'''Verifying format of diff output from assertEqual involving strings
Expand All @@ -1208,6 +1216,8 @@ def testAssertEqualMultipleLinesMismatchedNewlinesTerminators(self):
# need to remove the first line of the error message
error = str(e).split('\n', 1)[1]
self.assertEqual(sample_text_error, error)
else:
self.fail(f'{self.failureException} not raised')

def testEqualityBytesWarning(self):
if sys.flags.bytes_warning:
Expand Down

0 comments on commit 767c955

Please sign in to comment.