diff options
author | Michael Foord <fuzzyman@voidspace.org.uk> | 2009-05-02 11:43:06 (GMT) |
---|---|---|
committer | Michael Foord <fuzzyman@voidspace.org.uk> | 2009-05-02 11:43:06 (GMT) |
commit | 829f6b80529bcf66bf52bb9b301bf718118b8316 (patch) | |
tree | 7850c9f8dda5c01911fb32083f97b05f529e36e5 /Lib/test/test_unittest.py | |
parent | 27f204dc297945378f72536e63936bf3bb995fdf (diff) | |
download | cpython-829f6b80529bcf66bf52bb9b301bf718118b8316.zip cpython-829f6b80529bcf66bf52bb9b301bf718118b8316.tar.gz cpython-829f6b80529bcf66bf52bb9b301bf718118b8316.tar.bz2 |
Adds an exit parameter to unittest.main(). If False main no longer
calls sys.exit.
Closes issue 3379.
Michael Foord
Diffstat (limited to 'Lib/test/test_unittest.py')
-rw-r--r-- | Lib/test/test_unittest.py | 73 |
1 files changed, 71 insertions, 2 deletions
diff --git a/Lib/test/test_unittest.py b/Lib/test/test_unittest.py index f28b1af..bff5999 100644 --- a/Lib/test/test_unittest.py +++ b/Lib/test/test_unittest.py @@ -9,9 +9,10 @@ Still need testing: import re from test import test_support import unittest -from unittest import TestCase +from unittest import TestCase, TestProgram import types from copy import deepcopy +from cStringIO import StringIO ### Support code ################################################################ @@ -3040,6 +3041,73 @@ class TestLongMessage(TestCase): "^unexpectedly identical: None : oops$"]) +class Test_TestProgram(TestCase): + + # Horrible white box test + def testNoExit(self): + result = object() + test = object() + + class FakeRunner(object): + def run(self, test): + self.test = test + return result + + runner = FakeRunner() + + try: + oldParseArgs = TestProgram.parseArgs + TestProgram.parseArgs = lambda *args: None + TestProgram.test = test + + program = TestProgram(testRunner=runner, exit=False) + + self.assertEqual(program.result, result) + self.assertEqual(runner.test, test) + + finally: + TestProgram.parseArgs = oldParseArgs + del TestProgram.test + + + class FooBar(unittest.TestCase): + def testPass(self): + assert True + def testFail(self): + assert False + + class FooBarLoader(unittest.TestLoader): + """Test loader that returns a suite containing FooBar.""" + def loadTestsFromModule(self, module): + return self.suiteClass( + [self.loadTestsFromTestCase(Test_TestProgram.FooBar)]) + + + def test_NonExit(self): + program = unittest.main(exit=False, + testRunner=unittest.TextTestRunner(stream=StringIO()), + testLoader=self.FooBarLoader()) + self.assertTrue(hasattr(program, 'result')) + + + def test_Exit(self): + self.assertRaises( + SystemExit, + unittest.main, + testRunner=unittest.TextTestRunner(stream=StringIO()), + exit=True, + testLoader=self.FooBarLoader()) + + + def test_ExitAsDefault(self): + self.assertRaises( + SystemExit, + unittest.main, + testRunner=unittest.TextTestRunner(stream=StringIO()), + testLoader=self.FooBarLoader()) + + + ###################################################################### ## Main ###################################################################### @@ -3047,7 +3115,8 @@ class TestLongMessage(TestCase): def test_main(): test_support.run_unittest(Test_TestCase, Test_TestLoader, Test_TestSuite, Test_TestResult, Test_FunctionTestCase, - Test_TestSkipping, Test_Assertions, TestLongMessage) + Test_TestSkipping, Test_Assertions, TestLongMessage, + Test_TestProgram) if __name__ == "__main__": test_main() |