Skip to content

Commit 87c4e9c

Browse files
stefanorGlyphack
authored andcommitted
pythonGH-113661: unittest runner: Don't exit 5 if tests were skipped (python#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.
1 parent 22171a8 commit 87c4e9c

File tree

5 files changed

+18
-3
lines changed

5 files changed

+18
-3
lines changed

Doc/library/unittest.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2290,7 +2290,7 @@ Loading and running tests
22902290
The *testRunner* argument can either be a test runner class or an already
22912291
created instance of it. By default ``main`` calls :func:`sys.exit` with
22922292
an exit code indicating success (0) or failure (1) of the tests run.
2293-
An exit code of 5 indicates that no tests were run.
2293+
An exit code of 5 indicates that no tests were run or skipped.
22942294

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

Lib/test/test_unittest/test_program.py

Lines changed: 12 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ def runTests(self):
269269
testRunner = self.testRunner
270270
self.result = testRunner.run(self.test)
271271
if self.exit:
272-
if self.result.testsRun == 0:
272+
if self.result.testsRun == 0 and len(self.result.skipped) == 0:
273273
sys.exit(_NO_TESTS_EXITCODE)
274274
elif self.result.wasSuccessful():
275275
sys.exit(0)

Lib/unittest/runner.py

Lines changed: 1 addition & 1 deletion
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")
Lines changed: 3 additions & 0 deletions
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)