Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.12] gh-113267: Revert "gh-106584: Fix exit code for unittest in Python 3.12 (GH-106588)" (GH-114470) #114994

Merged
merged 1 commit into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/test/test_unittest/test_discovery.py
Original file line number Diff line number Diff line change
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, 0)
self.assertEqual(result.testsRun, 1)
self.assertEqual(import_calls, ['my_package'])

# Check picklability
Expand Down
12 changes: 6 additions & 6 deletions Lib/test/test_unittest/test_skipping.py
Original file line number Diff line number Diff line change
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 = ['addSkip', 'stopTest', 'startTest',
'addSuccess', 'stopTest']
expected = ['startTest', 'addSkip', 'stopTest',
'startTest', 'addSuccess', 'stopTest']
self.assertEqual(events, expected)
self.assertEqual(result.testsRun, 1)
self.assertEqual(result.testsRun, 2)
self.assertEqual(result.skipped, [(test_do_skip, "testing")])
self.assertTrue(result.wasSuccessful())

events = []
result = test_do_skip.run()
self.assertEqual(events, ['startTestRun', 'addSkip',
self.assertEqual(events, ['startTestRun', 'startTest', '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, ['addSkip', 'stopTest'])
self.assertEqual(events, ['startTest', 'addSkip', 'stopTest'])
self.assertEqual(result.skipped, [(test, "testing")])
self.assertEqual(record, [])

events = []
result = test.run()
self.assertEqual(events, ['startTestRun', 'addSkip',
self.assertEqual(events, ['startTestRun', 'startTest', 'addSkip',
'stopTest', 'stopTestRun'])
self.assertEqual(result.skipped, [(test, "testing")])
self.assertEqual(record, [])
Expand Down
4 changes: 1 addition & 3 deletions Lib/unittest/case.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,7 @@ 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 @@ -616,9 +617,6 @@ 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: 4 additions & 6 deletions Lib/unittest/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,10 @@ def _restoreStdout(self):

sys.stdout = self._original_stdout
sys.stderr = self._original_stderr
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()
self._stdout_buffer.seek(0)
self._stdout_buffer.truncate()
self._stderr_buffer.seek(0)
self._stderr_buffer.truncate()

def stopTestRun(self):
"""Called once after all tests are executed.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Revert changes in :gh:`106584` which made calls of ``TestResult`` methods
``startTest()`` and ``stopTest()`` unbalanced.
Loading