From 6e1be714a68fbef1d94125cb1768861e5424ad01 Mon Sep 17 00:00:00 2001 From: "Egor.Eliseev" Date: Mon, 10 Jul 2023 14:38:32 +0300 Subject: [PATCH 1/7] GH-106584: Fix exit code for unittest in Python 3.12 Increase the number of running tests only if it hasn't been skipped. --- Lib/test/test_unittest/test_discovery.py | 2 +- Lib/test/test_unittest/test_skipping.py | 12 ++++++------ Lib/unittest/case.py | 4 +++- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/Lib/test/test_unittest/test_discovery.py b/Lib/test/test_unittest/test_discovery.py index 946fa1258ea25e..e5cd929b5bb02a 100644 --- a/Lib/test/test_unittest/test_discovery.py +++ b/Lib/test/test_unittest/test_discovery.py @@ -572,7 +572,7 @@ def _get_module_from_name(name): 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 f146dcac18ecc0..1a6af06d32b433 100644 --- a/Lib/test/test_unittest/test_skipping.py +++ b/Lib/test/test_unittest/test_skipping.py @@ -103,16 +103,16 @@ def test_dont_skip(self): pass 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 @@ def test_1(self): 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 001b640dc43ad6..9a90af881bc6ad 100644 --- a/Lib/unittest/case.py +++ b/Lib/unittest/case.py @@ -606,7 +606,6 @@ def run(self, result=None): else: stopTestRun = None - result.startTest(self) try: testMethod = getattr(self, self._testMethodName) if (getattr(self.__class__, "__unittest_skip__", False) or @@ -617,6 +616,9 @@ def run(self, result=None): _addSkip(result, self, skip_why) return result + # Increase the number of running 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) From ee3af811e1a6a7cd2e4db1bce1688b18ca154423 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Tue, 11 Jul 2023 08:56:41 +0000 Subject: [PATCH 2/7] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2023-07-11-08-56-40.gh-issue-106584.g-SBtC.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2023-07-11-08-56-40.gh-issue-106584.g-SBtC.rst 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 00000000000000..3dfb4b69cd0d5d --- /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` in Python 3.12 or greater if all +tests are skipped. Patch by Egor Eliseev. From 9779a2b09e8a7d9c22ae471b242e4cb9f601c58a Mon Sep 17 00:00:00 2001 From: sunmy2019 <59365878+sunmy2019@users.noreply.github.com> Date: Tue, 11 Jul 2023 17:50:11 +0800 Subject: [PATCH 3/7] Update Misc/NEWS.d/next/Library/2023-07-11-08-56-40.gh-issue-106584.g-SBtC.rst --- .../Library/2023-07-11-08-56-40.gh-issue-106584.g-SBtC.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 index 3dfb4b69cd0d5d..08330e28b03d16 100644 --- 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 @@ -1,2 +1,2 @@ -Fix exit code for `unittest` in Python 3.12 or greater if all -tests are skipped. Patch by Egor Eliseev. +Fix exit code for `unittest` in Python 3.12 or greater if all +tests are skipped. Patch by Egor Eliseev. From 43248a95ab3bfdbd76592cdd564ec79ba0ba520e Mon Sep 17 00:00:00 2001 From: EliseevEgor Date: Tue, 11 Jul 2023 13:02:27 +0300 Subject: [PATCH 4/7] GH-106584: Apply comment suggestion Co-authored-by: sunmy2019 <59365878+sunmy2019@users.noreply.github.com> --- Lib/unittest/case.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/unittest/case.py b/Lib/unittest/case.py index 9a90af881bc6ad..811557498bb30e 100644 --- a/Lib/unittest/case.py +++ b/Lib/unittest/case.py @@ -616,7 +616,7 @@ def run(self, result=None): _addSkip(result, self, skip_why) return result - # Increase the number of running tests only if it hasn't been skipped + # Increase the number of tests only if it hasn't been skipped result.startTest(self) expecting_failure = ( From 901cf01e319c29ef3a2b36826f2534de6cc985a1 Mon Sep 17 00:00:00 2001 From: EliseevEgor Date: Tue, 11 Jul 2023 14:04:12 +0300 Subject: [PATCH 5/7] GH-106584: Remove the version info in the NEWS Co-authored-by: Nikita Sobolev --- .../Library/2023-07-11-08-56-40.gh-issue-106584.g-SBtC.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 index 08330e28b03d16..e65a18f54d7952 100644 --- 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 @@ -1,2 +1,3 @@ -Fix exit code for `unittest` in Python 3.12 or greater if all +Fix exit code for ``unittest`` if all + tests are skipped. Patch by Egor Eliseev. From 1ab5cfa3be95811aa6fa558d5fe50a1c2690cf44 Mon Sep 17 00:00:00 2001 From: "Egor.Eliseev" Date: Tue, 11 Jul 2023 14:17:19 +0300 Subject: [PATCH 6/7] GH-106584: Remove empty line in the NEWS --- .../Library/2023-07-11-08-56-40.gh-issue-106584.g-SBtC.rst | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) 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 index e65a18f54d7952..a13b61bf1c121b 100644 --- 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 @@ -1,3 +1,2 @@ -Fix exit code for ``unittest`` if all - -tests are skipped. Patch by Egor Eliseev. +Fix exit code for ``unittest`` if all tests are skipped. +Patch by Egor Eliseev. From 6313c98278b3add084c8ef2728754ced0cde30f6 Mon Sep 17 00:00:00 2001 From: "Egor.Eliseev" Date: Tue, 11 Jul 2023 17:37:27 +0300 Subject: [PATCH 7/7] GH-106584: Check buffers before seek --- Lib/unittest/result.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Lib/unittest/result.py b/Lib/unittest/result.py index 7757dba9670b43..bf206e6d875380 100644 --- a/Lib/unittest/result.py +++ b/Lib/unittest/result.py @@ -97,10 +97,12 @@ def _restoreStdout(self): 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.