gh-136442: Fix unittest to return exit code 5 when setUpClass raises an exception (#136487)

This commit is contained in:
Takuya UESHIN 2025-11-14 16:59:51 -08:00 committed by GitHub
parent 453d886f85
commit 53d65c840e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 26 additions and 5 deletions

View file

@ -75,6 +75,14 @@ def testUnexpectedSuccess(self):
class Empty(unittest.TestCase):
pass
class SetUpClassFailure(unittest.TestCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
raise Exception
def testPass(self):
pass
class TestLoader(unittest.TestLoader):
"""Test loader that returns a suite containing the supplied testcase."""
@ -191,6 +199,18 @@ def test_ExitEmptySuite(self):
out = stream.getvalue()
self.assertIn('\nNO TESTS RAN\n', out)
def test_ExitSetUpClassFailureSuite(self):
stream = BufferedWriter()
with self.assertRaises(SystemExit) as cm:
unittest.main(
argv=["setup_class_failure"],
testRunner=unittest.TextTestRunner(stream=stream),
testLoader=self.TestLoader(self.SetUpClassFailure))
self.assertEqual(cm.exception.code, 1)
out = stream.getvalue()
self.assertIn("ERROR: setUpClass", out)
self.assertIn("SetUpClassFailure", out)
class InitialisableProgram(unittest.TestProgram):
exit = False