Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 159e3db

Browse files
[3.12] GH-113661: unittest runner: Don't exit 5 if tests were skipped (GH-113856) (#113875)
GH-113661: unittest runner: Don't exit 5 if tests were skipped (GH-113856) The intention of exiting 5 was to detect issues where the test suite wasn't discovered at all. If we skipped tests, it was correctly discovered. (cherry picked from commit 3a9096c) Co-authored-by: Stefano Rivera <[email protected]>
1 parent 85cf360 commit 159e3db

File tree

5 files changed

+18
-3
lines changed

5 files changed

+18
-3
lines changed

Doc/library/unittest.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -2282,7 +2282,7 @@ Loading and running tests
22822282
The *testRunner* argument can either be a test runner class or an already
22832283
created instance of it. By default ``main`` calls :func:`sys.exit` with
22842284
an exit code indicating success (0) or failure (1) of the tests run.
2285-
An exit code of 5 indicates that no tests were run.
2285+
An exit code of 5 indicates that no tests were run or skipped.
22862286

22872287
The *testLoader* argument has to be a :class:`TestLoader` instance,
22882288
and defaults to :data:`defaultTestLoader`.

Lib/test/test_unittest/test_program.py

+12
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,18 @@ def test_ExitAsDefault(self):
167167
'expected failures=1, unexpected successes=1)\n')
168168
self.assertTrue(out.endswith(expected))
169169

170+
def test_ExitSkippedSuite(self):
171+
stream = BufferedWriter()
172+
with self.assertRaises(SystemExit) as cm:
173+
unittest.main(
174+
argv=["foobar", "-k", "testSkipped"],
175+
testRunner=unittest.TextTestRunner(stream=stream),
176+
testLoader=self.TestLoader(self.FooBar))
177+
self.assertEqual(cm.exception.code, 0)
178+
out = stream.getvalue()
179+
expected = '\n\nOK (skipped=1)\n'
180+
self.assertTrue(out.endswith(expected))
181+
170182
def test_ExitEmptySuite(self):
171183
stream = BufferedWriter()
172184
with self.assertRaises(SystemExit) as cm:

Lib/unittest/main.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ def runTests(self):
280280
testRunner = self.testRunner
281281
self.result = testRunner.run(self.test)
282282
if self.exit:
283-
if self.result.testsRun == 0:
283+
if self.result.testsRun == 0 and len(self.result.skipped) == 0:
284284
sys.exit(_NO_TESTS_EXITCODE)
285285
elif self.result.wasSuccessful():
286286
sys.exit(0)

Lib/unittest/runner.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ def run(self, test):
274274
infos.append("failures=%d" % failed)
275275
if errored:
276276
infos.append("errors=%d" % errored)
277-
elif run == 0:
277+
elif run == 0 and not skipped:
278278
self.stream.write("NO TESTS RAN")
279279
else:
280280
self.stream.write("OK")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
unittest runner: Don't exit 5 if tests were skipped. The intention of
2+
exiting 5 was to detect issues where the test suite wasn't discovered at
3+
all. If we skipped tests, it was correctly discovered.

0 commit comments

Comments
 (0)