]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] GH-113661: unittest runner: Don't exit 5 if tests were skipped (GH-113856...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Tue, 9 Jan 2024 21:34:38 +0000 (22:34 +0100)
committerGitHub <noreply@github.com>
Tue, 9 Jan 2024 21:34:38 +0000 (13:34 -0800)
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 3a9096c337c16c9335e0d4eba8d1d4196258af72)

Co-authored-by: Stefano Rivera <stefano@rivera.za.net>
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 f821688d145b4fb5afbf70c6147d3cc8263c5ad8..7b45f33bff100c9dbc6f03b27f6611521e53fcf0 100644 (file)
@@ -2282,7 +2282,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 51b81a6c3728bbf35d0ddf47faec93ce63fd55e5..dd4dbf7535f06beffd9b8e7a5380d03b548bcad0 100644 (file)
@@ -280,7 +280,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.