Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 42ebdd8

Browse files
authored
gh-120544: Add else: fail() to tests where exception is expected (#120545)
1 parent d4039d3 commit 42ebdd8

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

Lib/test/test_exceptions.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1859,6 +1859,8 @@ def f():
18591859
except self.failureException:
18601860
with support.captured_stderr() as err:
18611861
sys.__excepthook__(*sys.exc_info())
1862+
else:
1863+
self.fail("assertRaisesRegex should have failed.")
18621864

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

Lib/test/test_unittest/test_case.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,6 +1151,8 @@ def testAssertMultiLineEqual(self):
11511151
# need to remove the first line of the error message
11521152
error = str(e).split('\n', 1)[1]
11531153
self.assertEqual(sample_text_error, error)
1154+
else:
1155+
self.fail(f'{self.failureException} not raised')
11541156

11551157
def testAssertEqualSingleLine(self):
11561158
sample_text = "laden swallows fly slowly"
@@ -1167,6 +1169,8 @@ def testAssertEqualSingleLine(self):
11671169
# need to remove the first line of the error message
11681170
error = str(e).split('\n', 1)[1]
11691171
self.assertEqual(sample_text_error, error)
1172+
else:
1173+
self.fail(f'{self.failureException} not raised')
11701174

11711175
def testAssertEqualwithEmptyString(self):
11721176
'''Verify when there is an empty string involved, the diff output
@@ -1184,6 +1188,8 @@ def testAssertEqualwithEmptyString(self):
11841188
# need to remove the first line of the error message
11851189
error = str(e).split('\n', 1)[1]
11861190
self.assertEqual(sample_text_error, error)
1191+
else:
1192+
self.fail(f'{self.failureException} not raised')
11871193

11881194
def testAssertEqualMultipleLinesMissingNewlineTerminator(self):
11891195
'''Verifying format of diff output from assertEqual involving strings
@@ -1204,6 +1210,8 @@ def testAssertEqualMultipleLinesMissingNewlineTerminator(self):
12041210
# need to remove the first line of the error message
12051211
error = str(e).split('\n', 1)[1]
12061212
self.assertEqual(sample_text_error, error)
1213+
else:
1214+
self.fail(f'{self.failureException} not raised')
12071215

12081216
def testAssertEqualMultipleLinesMismatchedNewlinesTerminators(self):
12091217
'''Verifying format of diff output from assertEqual involving strings
@@ -1227,6 +1235,8 @@ def testAssertEqualMultipleLinesMismatchedNewlinesTerminators(self):
12271235
# need to remove the first line of the error message
12281236
error = str(e).split('\n', 1)[1]
12291237
self.assertEqual(sample_text_error, error)
1238+
else:
1239+
self.fail(f'{self.failureException} not raised')
12301240

12311241
def testEqualityBytesWarning(self):
12321242
if sys.flags.bytes_warning:

0 commit comments

Comments
 (0)