Skip to content

Commit

Permalink
gh-120544: Add else: fail() to tests where exception is expected (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
sobolevn authored Jun 15, 2024
1 parent d4039d3 commit 42ebdd8
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 @@ -1859,6 +1859,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 @@ -1151,6 +1151,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 @@ -1167,6 +1169,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 @@ -1184,6 +1188,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 @@ -1204,6 +1210,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 @@ -1227,6 +1235,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 42ebdd8

Please sign in to comment.