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

Skip to content

Commit 767c955

Browse files
[3.13] gh-120544: Add else: fail() to tests where exception is expected (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 <[email protected]>
1 parent e54a28b commit 767c955

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
@@ -1836,6 +1836,8 @@ def f():
18361836
except self.failureException:
18371837
with support.captured_stderr() as err:
18381838
sys.__excepthook__(*sys.exc_info())
1839+
else:
1840+
self.fail("assertRaisesRegex should have failed.")
18391841

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

Lib/test/test_unittest/test_case.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,6 +1132,8 @@ def testAssertMultiLineEqual(self):
11321132
# need to remove the first line of the error message
11331133
error = str(e).split('\n', 1)[1]
11341134
self.assertEqual(sample_text_error, error)
1135+
else:
1136+
self.fail(f'{self.failureException} not raised')
11351137

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

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

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

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

12121222
def testEqualityBytesWarning(self):
12131223
if sys.flags.bytes_warning:

0 commit comments

Comments
 (0)