]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-113661: unittest runner: Don't exit 5 if tests were skipped (#113856)
authorStefano Rivera <stefano@rivera.za.net>
Tue, 9 Jan 2024 19:50:01 +0000 (11:50 -0800)
committerGitHub <noreply@github.com>
Tue, 9 Jan 2024 19:50:01 +0000 (19:50 +0000)
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.

Doc/library/unittest.rst
Lib/test/test_unittest/test_program.py
Lib/unittest/main.py
Lib/unittest/runner.py
Misc/NEWS.d/next/Library/2024-01-09-08-59-43.gh-issue-113661.asvXSx.rst [new file with mode: 0644]

index 70b4c84c05f818a69b3c88aa17768ce8e2cc789b..491009769f5aa6862564c97b660ddf70a303dc11 100644 (file)
@@ -2290,7 +2290,7 @@ Loading and running tests
    The *testRunner* argument can either be a test runner class or an already
    created instance of it. By default ``main`` calls :func:`sys.exit` with
    an exit code indicating success (0) or failure (1) of the tests run.
-   An exit code of 5 indicates that no tests were run.
+   An exit code of 5 indicates that no tests were run or skipped.
 
    The *testLoader* argument has to be a :class:`TestLoader` instance,
    and defaults to :data:`defaultTestLoader`.
index f6d52f93e4a25f96d6d1403ab40aba87544089da..d8f5d3692a5088905a3d1a37a2c808239a33827b 100644 (file)
@@ -167,6 +167,18 @@ class Test_TestProgram(unittest.TestCase):
                     'expected failures=1, unexpected successes=1)\n')
         self.assertTrue(out.endswith(expected))
 
+    def test_ExitSkippedSuite(self):
+        stream = BufferedWriter()
+        with self.assertRaises(SystemExit) as cm:
+            unittest.main(
+                argv=["foobar", "-k", "testSkipped"],
+                testRunner=unittest.TextTestRunner(stream=stream),
+                testLoader=self.TestLoader(self.FooBar))
+        self.assertEqual(cm.exception.code, 0)
+        out = stream.getvalue()
+        expected = '\n\nOK (skipped=1)\n'
+        self.assertTrue(out.endswith(expected))
+
     def test_ExitEmptySuite(self):
         stream = BufferedWriter()
         with self.assertRaises(SystemExit) as cm:
index d29a9f91fcca420361467eacde1e623c3cceb853..c3869de3f6f18e111585a776b961d9a9986a1161 100644 (file)
@@ -269,7 +269,7 @@ class TestProgram(object):
             testRunner = self.testRunner
         self.result = testRunner.run(self.test)
         if self.exit:
-            if self.result.testsRun == 0:
+            if self.result.testsRun == 0 and len(self.result.skipped) == 0:
                 sys.exit(_NO_TESTS_EXITCODE)
             elif self.result.wasSuccessful():
                 sys.exit(0)
index e3c020e0ace96de1641bc6fdfab09aa2861908f9..2bcadf0c998bd9b04bb6f2cb19a2fa12a5f35c7e 100644 (file)
@@ -274,7 +274,7 @@ class TextTestRunner(object):
                 infos.append("failures=%d" % failed)
             if errored:
                 infos.append("errors=%d" % errored)
-        elif run == 0:
+        elif run == 0 and not skipped:
             self.stream.write("NO TESTS RAN")
         else:
             self.stream.write("OK")
diff --git a/Misc/NEWS.d/next/Library/2024-01-09-08-59-43.gh-issue-113661.asvXSx.rst b/Misc/NEWS.d/next/Library/2024-01-09-08-59-43.gh-issue-113661.asvXSx.rst
new file mode 100644 (file)
index 0000000..f4a4f1a
--- /dev/null
@@ -0,0 +1,3 @@
+unittest runner: Don't exit 5 if tests were skipped. 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.