Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Lib/test/test_unittest/test_program.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""

Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions Lib/unittest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,12 @@ def runTests(self):
testRunner = self.testRunner
self.result = testRunner.run(self.test)
if self.exit:
if self.result.testsRun == 0 and len(self.result.skipped) == 0:
if not self.result.wasSuccessful():
sys.exit(1)
elif self.result.testsRun == 0 and len(self.result.skipped) == 0:
sys.exit(_NO_TESTS_EXITCODE)
elif self.result.wasSuccessful():
sys.exit(0)
else:
sys.exit(1)
sys.exit(0)


main = TestProgram
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Use exitcode ``1`` instead of ``5`` if :func:`unittest.TestCase.setUpClass` raises an exception
Loading