From 8fc071345b50dd3de61ebeeaa287ccef21d061b2 Mon Sep 17 00:00:00 2001 From: EliseevEgor Date: Fri, 22 Sep 2023 15:26:27 +0300 Subject: [PATCH] gh-106584: Fix exit code for unittest in Python 3.12 (#106588) Co-authored-by: sunmy2019 <59365878+sunmy2019@users.noreply.github.com> Co-authored-by: Nikita Sobolev --- Lib/test/test_unittest/test_discovery.py | 2 +- Lib/test/test_unittest/test_skipping.py | 12 ++++++------ Lib/unittest/case.py | 4 +++- Lib/unittest/result.py | 10 ++++++---- .../2023-07-11-08-56-40.gh-issue-106584.g-SBtC.rst | 2 ++ 5 files changed, 18 insertions(+), 12 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2023-07-11-08-56-40.gh-issue-106584.g-SBtC.rst diff --git a/Lib/test/test_unittest/test_discovery.py b/Lib/test/test_unittest/test_discovery.py index 004898ed431..dcb72d73efc 100644 --- a/Lib/test/test_unittest/test_discovery.py +++ b/Lib/test/test_unittest/test_discovery.py @@ -571,7 +571,7 @@ class TestDiscovery(unittest.TestCase): result = unittest.TestResult() suite.run(result) self.assertEqual(len(result.skipped), 1) - self.assertEqual(result.testsRun, 1) + self.assertEqual(result.testsRun, 0) self.assertEqual(import_calls, ['my_package']) # Check picklability diff --git a/Lib/test/test_unittest/test_skipping.py b/Lib/test/test_unittest/test_skipping.py index f146dcac18e..1a6af06d32b 100644 --- a/Lib/test/test_unittest/test_skipping.py +++ b/Lib/test/test_unittest/test_skipping.py @@ -103,16 +103,16 @@ class Test_TestSkipping(unittest.TestCase): result = LoggingResult(events) self.assertIs(suite.run(result), result) self.assertEqual(len(result.skipped), 1) - expected = ['startTest', 'addSkip', 'stopTest', - 'startTest', 'addSuccess', 'stopTest'] + expected = ['addSkip', 'stopTest', 'startTest', + 'addSuccess', 'stopTest'] self.assertEqual(events, expected) - self.assertEqual(result.testsRun, 2) + self.assertEqual(result.testsRun, 1) self.assertEqual(result.skipped, [(test_do_skip, "testing")]) self.assertTrue(result.wasSuccessful()) events = [] result = test_do_skip.run() - self.assertEqual(events, ['startTestRun', 'startTest', 'addSkip', + self.assertEqual(events, ['startTestRun', 'addSkip', 'stopTest', 'stopTestRun']) self.assertEqual(result.skipped, [(test_do_skip, "testing")]) @@ -135,13 +135,13 @@ class Test_TestSkipping(unittest.TestCase): test = Foo("test_1") suite = unittest.TestSuite([test]) self.assertIs(suite.run(result), result) - self.assertEqual(events, ['startTest', 'addSkip', 'stopTest']) + self.assertEqual(events, ['addSkip', 'stopTest']) self.assertEqual(result.skipped, [(test, "testing")]) self.assertEqual(record, []) events = [] result = test.run() - self.assertEqual(events, ['startTestRun', 'startTest', 'addSkip', + self.assertEqual(events, ['startTestRun', 'addSkip', 'stopTest', 'stopTestRun']) self.assertEqual(result.skipped, [(test, "testing")]) self.assertEqual(record, []) diff --git a/Lib/unittest/case.py b/Lib/unittest/case.py index 001b640dc43..811557498bb 100644 --- a/Lib/unittest/case.py +++ b/Lib/unittest/case.py @@ -606,7 +606,6 @@ class TestCase(object): else: stopTestRun = None - result.startTest(self) try: testMethod = getattr(self, self._testMethodName) if (getattr(self.__class__, "__unittest_skip__", False) or @@ -617,6 +616,9 @@ class TestCase(object): _addSkip(result, self, skip_why) return result + # Increase the number of tests only if it hasn't been skipped + result.startTest(self) + expecting_failure = ( getattr(self, "__unittest_expecting_failure__", False) or getattr(testMethod, "__unittest_expecting_failure__", False) diff --git a/Lib/unittest/result.py b/Lib/unittest/result.py index 3ace0a5b7bf..9e56f658027 100644 --- a/Lib/unittest/result.py +++ b/Lib/unittest/result.py @@ -97,10 +97,12 @@ class TestResult(object): sys.stdout = self._original_stdout sys.stderr = self._original_stderr - self._stdout_buffer.seek(0) - self._stdout_buffer.truncate() - self._stderr_buffer.seek(0) - self._stderr_buffer.truncate() + if self._stdout_buffer is not None: + self._stdout_buffer.seek(0) + self._stdout_buffer.truncate() + if self._stderr_buffer is not None: + self._stderr_buffer.seek(0) + self._stderr_buffer.truncate() def stopTestRun(self): """Called once after all tests are executed. diff --git a/Misc/NEWS.d/next/Library/2023-07-11-08-56-40.gh-issue-106584.g-SBtC.rst b/Misc/NEWS.d/next/Library/2023-07-11-08-56-40.gh-issue-106584.g-SBtC.rst new file mode 100644 index 00000000000..a13b61bf1c1 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-07-11-08-56-40.gh-issue-106584.g-SBtC.rst @@ -0,0 +1,2 @@ +Fix exit code for ``unittest`` if all tests are skipped. +Patch by Egor Eliseev.