[Python-checkins] bpo-22815: Print unexpected successes in summary in TextTestResult (GH-30138)

serhiy-storchaka webhook-mailer at python.org
Sun Dec 26 06:22:48 EST 2021


https://github.com/python/cpython/commit/1944434b44e0118e812bf63f47b268ff6dd0c8f1
commit: 1944434b44e0118e812bf63f47b268ff6dd0c8f1
branch: main
author: Serhiy Storchaka <storchaka at gmail.com>
committer: serhiy-storchaka <storchaka at gmail.com>
date: 2021-12-26T13:22:44+02:00
summary:

bpo-22815: Print unexpected successes in summary in TextTestResult (GH-30138)

files:
A Misc/NEWS.d/next/Library/2021-12-16-12-54-21.bpo-22815.0NRH8s.rst
M Lib/unittest/runner.py
M Lib/unittest/test/test_program.py

diff --git a/Lib/unittest/runner.py b/Lib/unittest/runner.py
index 120ee6aba3570..6678adb6a7d81 100644
--- a/Lib/unittest/runner.py
+++ b/Lib/unittest/runner.py
@@ -142,6 +142,12 @@ def printErrors(self):
             self.stream.flush()
         self.printErrorList('ERROR', self.errors)
         self.printErrorList('FAIL', self.failures)
+        unexpectedSuccesses = getattr(self, 'unexpectedSuccesses', ())
+        if unexpectedSuccesses:
+            self.stream.writeln(self.separator1)
+            for test in unexpectedSuccesses:
+                self.stream.writeln(f"UNEXPECTED SUCCESS: {self.getDescription(test)}")
+            self.stream.flush()
 
     def printErrorList(self, flavour, errors):
         for test, err in errors:
diff --git a/Lib/unittest/test/test_program.py b/Lib/unittest/test/test_program.py
index 2bf7dd72ed21c..f7049fbb24e7b 100644
--- a/Lib/unittest/test/test_program.py
+++ b/Lib/unittest/test/test_program.py
@@ -61,6 +61,17 @@ def testPass(self):
             pass
         def testFail(self):
             raise AssertionError
+        def testError(self):
+            1/0
+        @unittest.skip('skipping')
+        def testSkipped(self):
+            raise AssertionError
+        @unittest.expectedFailure
+        def testExpectedFailure(self):
+            raise AssertionError
+        @unittest.expectedFailure
+        def testUnexpectedSuccess(self):
+            pass
 
     class FooBarLoader(unittest.TestLoader):
         """Test loader that returns a suite containing FooBar."""
@@ -111,9 +122,13 @@ def test_NonExit(self):
                                 testRunner=unittest.TextTestRunner(stream=stream),
                                 testLoader=self.FooBarLoader())
         self.assertTrue(hasattr(program, 'result'))
-        self.assertIn('\nFAIL: testFail ', stream.getvalue())
-        self.assertTrue(stream.getvalue().endswith('\n\nFAILED (failures=1)\n'))
-
+        out = stream.getvalue()
+        self.assertIn('\nFAIL: testFail ', out)
+        self.assertIn('\nERROR: testError ', out)
+        self.assertIn('\nUNEXPECTED SUCCESS: testUnexpectedSuccess ', out)
+        expected = ('\n\nFAILED (failures=1, errors=1, skipped=1, '
+                    'expected failures=1, unexpected successes=1)\n')
+        self.assertTrue(out.endswith(expected))
 
     def test_Exit(self):
         stream = BufferedWriter()
@@ -124,9 +139,13 @@ def test_Exit(self):
             testRunner=unittest.TextTestRunner(stream=stream),
             exit=True,
             testLoader=self.FooBarLoader())
-        self.assertIn('\nFAIL: testFail ', stream.getvalue())
-        self.assertTrue(stream.getvalue().endswith('\n\nFAILED (failures=1)\n'))
-
+        out = stream.getvalue()
+        self.assertIn('\nFAIL: testFail ', out)
+        self.assertIn('\nERROR: testError ', out)
+        self.assertIn('\nUNEXPECTED SUCCESS: testUnexpectedSuccess ', out)
+        expected = ('\n\nFAILED (failures=1, errors=1, skipped=1, '
+                    'expected failures=1, unexpected successes=1)\n')
+        self.assertTrue(out.endswith(expected))
 
     def test_ExitAsDefault(self):
         stream = BufferedWriter()
@@ -136,8 +155,13 @@ def test_ExitAsDefault(self):
             argv=["foobar"],
             testRunner=unittest.TextTestRunner(stream=stream),
             testLoader=self.FooBarLoader())
-        self.assertIn('\nFAIL: testFail ', stream.getvalue())
-        self.assertTrue(stream.getvalue().endswith('\n\nFAILED (failures=1)\n'))
+        out = stream.getvalue()
+        self.assertIn('\nFAIL: testFail ', out)
+        self.assertIn('\nERROR: testError ', out)
+        self.assertIn('\nUNEXPECTED SUCCESS: testUnexpectedSuccess ', out)
+        expected = ('\n\nFAILED (failures=1, errors=1, skipped=1, '
+                    'expected failures=1, unexpected successes=1)\n')
+        self.assertTrue(out.endswith(expected))
 
 
 class InitialisableProgram(unittest.TestProgram):
diff --git a/Misc/NEWS.d/next/Library/2021-12-16-12-54-21.bpo-22815.0NRH8s.rst b/Misc/NEWS.d/next/Library/2021-12-16-12-54-21.bpo-22815.0NRH8s.rst
new file mode 100644
index 0000000000000..5c4600f316ac3
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-12-16-12-54-21.bpo-22815.0NRH8s.rst
@@ -0,0 +1,2 @@
+Print unexpected successes together with failures and errors in summary in
+:class:`unittest.TextTestResult`.



More information about the Python-checkins mailing list