]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-120544: Add `else: fail()` to tests where exception is expected (#120545)
authorNikita Sobolev <mail@sobolevn.me>
Sat, 15 Jun 2024 10:33:14 +0000 (13:33 +0300)
committerGitHub <noreply@github.com>
Sat, 15 Jun 2024 10:33:14 +0000 (10:33 +0000)
Lib/test/test_exceptions.py
Lib/test/test_unittest/test_case.py

index 9460d1f1c864b90043b46588831b36f56c474ded..e4f2e3a97b8bb8f15379b233ce124c4fcb34e624 100644 (file)
@@ -1859,6 +1859,8 @@ class NameErrorTests(unittest.TestCase):
         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())
 
index 174209094021071cef374c518e9ef7b86c5f213b..b4b2194a09cf9ff019ce39994d8461795653fc2d 100644 (file)
@@ -1151,6 +1151,8 @@ test case
             # 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"
@@ -1167,6 +1169,8 @@ test case
             # 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
@@ -1184,6 +1188,8 @@ test case
             # 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
@@ -1204,6 +1210,8 @@ test case
             # 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
@@ -1227,6 +1235,8 @@ test case
             # 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: