diff options
author | Michael Foord <fuzzyman@voidspace.org.uk> | 2010-03-25 23:56:33 (GMT) |
---|---|---|
committer | Michael Foord <fuzzyman@voidspace.org.uk> | 2010-03-25 23:56:33 (GMT) |
commit | 35b3792ed3598d9cc52160604ed0e4efa2b1af0b (patch) | |
tree | 0f4d47301378d2f2849a2a35d638b62c520eaf1d /Lib/unittest/test/test_program.py | |
parent | fbe519975bc97e12c875df4bda891c819bb3b8ec (diff) | |
download | cpython-35b3792ed3598d9cc52160604ed0e4efa2b1af0b.zip cpython-35b3792ed3598d9cc52160604ed0e4efa2b1af0b.tar.gz cpython-35b3792ed3598d9cc52160604ed0e4efa2b1af0b.tar.bz2 |
Turn unittest tests into a package
Diffstat (limited to 'Lib/unittest/test/test_program.py')
-rw-r--r-- | Lib/unittest/test/test_program.py | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/Lib/unittest/test/test_program.py b/Lib/unittest/test/test_program.py new file mode 100644 index 0000000..a3934dd --- /dev/null +++ b/Lib/unittest/test/test_program.py @@ -0,0 +1,78 @@ +from cStringIO import StringIO + +import unittest + + +class Test_TestProgram(unittest.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() + + oldParseArgs = unittest.TestProgram.parseArgs + def restoreParseArgs(): + unittest.TestProgram.parseArgs = oldParseArgs + unittest.TestProgram.parseArgs = lambda *args: None + self.addCleanup(restoreParseArgs) + + def removeTest(): + del unittest.TestProgram.test + unittest.TestProgram.test = test + self.addCleanup(removeTest) + + program = unittest.TestProgram(testRunner=runner, exit=False, verbosity=2) + + self.assertEqual(program.result, result) + self.assertEqual(runner.test, test) + self.assertEqual(program.verbosity, 2) + + 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, + argv=["foobar"], + testRunner=unittest.TextTestRunner(stream=StringIO()), + testLoader=self.FooBarLoader()) + self.assertTrue(hasattr(program, 'result')) + + + def test_Exit(self): + self.assertRaises( + SystemExit, + unittest.main, + argv=["foobar"], + testRunner=unittest.TextTestRunner(stream=StringIO()), + exit=True, + testLoader=self.FooBarLoader()) + + + def test_ExitAsDefault(self): + self.assertRaises( + SystemExit, + unittest.main, + argv=["foobar"], + testRunner=unittest.TextTestRunner(stream=StringIO()), + testLoader=self.FooBarLoader()) + + +if __name__ == '__main__': + unittest.main() |