Skip to content

Commit

Permalink
[3.12] gh-106584: Fix exit code for unittest in Python 3.12 (GH-106588)…
Browse files Browse the repository at this point in the history
… (#109725)

gh-106584: Fix exit code for unittest in Python 3.12 (GH-106588)
(cherry picked from commit 8fc0713)

Co-authored-by: EliseevEgor <egor.eliseev@jetbrains.com>
Co-authored-by: sunmy2019 <59365878+sunmy2019@users.noreply.github.com>
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
  • Loading branch information
4 people committed Oct 2, 2023
1 parent 84ef145 commit 551aa6a
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Lib/test/test_unittest/test_discovery.py
Expand Up @@ -571,7 +571,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
Expand Down
12 changes: 6 additions & 6 deletions Lib/test/test_unittest/test_skipping.py
Expand Up @@ -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")])

Expand All @@ -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, [])
Expand Down
4 changes: 3 additions & 1 deletion Lib/unittest/case.py
Expand Up @@ -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
Expand All @@ -617,6 +616,9 @@ def run(self, result=None):
_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)
Expand Down
10 changes: 6 additions & 4 deletions Lib/unittest/result.py
Expand Up @@ -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.
Expand Down
@@ -0,0 +1,2 @@
Fix exit code for ``unittest`` if all tests are skipped.
Patch by Egor Eliseev.

0 comments on commit 551aa6a

Please sign in to comment.