[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 42ebdd83bb)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
This commit is contained in:
Miss Islington (bot) 2024-06-15 13:54:07 +02:00 committed by GitHub
parent e54a28bcc9
commit 767c955544
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 12 additions and 0 deletions

View file

@ -1836,6 +1836,8 @@ class NameErrorTests(unittest.TestCase):
except self.failureException: except self.failureException:
with support.captured_stderr() as err: with support.captured_stderr() as err:
sys.__excepthook__(*sys.exc_info()) sys.__excepthook__(*sys.exc_info())
else:
self.fail("assertRaisesRegex should have failed.")
self.assertIn("aab", err.getvalue()) self.assertIn("aab", err.getvalue())

View file

@ -1132,6 +1132,8 @@ test case
# need to remove the first line of the error message # need to remove the first line of the error message
error = str(e).split('\n', 1)[1] error = str(e).split('\n', 1)[1]
self.assertEqual(sample_text_error, error) self.assertEqual(sample_text_error, error)
else:
self.fail(f'{self.failureException} not raised')
def testAssertEqualSingleLine(self): def testAssertEqualSingleLine(self):
sample_text = "laden swallows fly slowly" sample_text = "laden swallows fly slowly"
@ -1148,6 +1150,8 @@ test case
# need to remove the first line of the error message # need to remove the first line of the error message
error = str(e).split('\n', 1)[1] error = str(e).split('\n', 1)[1]
self.assertEqual(sample_text_error, error) self.assertEqual(sample_text_error, error)
else:
self.fail(f'{self.failureException} not raised')
def testAssertEqualwithEmptyString(self): def testAssertEqualwithEmptyString(self):
'''Verify when there is an empty string involved, the diff output '''Verify when there is an empty string involved, the diff output
@ -1165,6 +1169,8 @@ test case
# need to remove the first line of the error message # need to remove the first line of the error message
error = str(e).split('\n', 1)[1] error = str(e).split('\n', 1)[1]
self.assertEqual(sample_text_error, error) self.assertEqual(sample_text_error, error)
else:
self.fail(f'{self.failureException} not raised')
def testAssertEqualMultipleLinesMissingNewlineTerminator(self): def testAssertEqualMultipleLinesMissingNewlineTerminator(self):
'''Verifying format of diff output from assertEqual involving strings '''Verifying format of diff output from assertEqual involving strings
@ -1185,6 +1191,8 @@ test case
# need to remove the first line of the error message # need to remove the first line of the error message
error = str(e).split('\n', 1)[1] error = str(e).split('\n', 1)[1]
self.assertEqual(sample_text_error, error) self.assertEqual(sample_text_error, error)
else:
self.fail(f'{self.failureException} not raised')
def testAssertEqualMultipleLinesMismatchedNewlinesTerminators(self): def testAssertEqualMultipleLinesMismatchedNewlinesTerminators(self):
'''Verifying format of diff output from assertEqual involving strings '''Verifying format of diff output from assertEqual involving strings
@ -1208,6 +1216,8 @@ test case
# need to remove the first line of the error message # need to remove the first line of the error message
error = str(e).split('\n', 1)[1] error = str(e).split('\n', 1)[1]
self.assertEqual(sample_text_error, error) self.assertEqual(sample_text_error, error)
else:
self.fail(f'{self.failureException} not raised')
def testEqualityBytesWarning(self): def testEqualityBytesWarning(self):
if sys.flags.bytes_warning: if sys.flags.bytes_warning: