diff options
author | Michael Foord <fuzzyman@voidspace.org.uk> | 2010-05-07 16:00:30 (GMT) |
---|---|---|
committer | Michael Foord <fuzzyman@voidspace.org.uk> | 2010-05-07 16:00:30 (GMT) |
commit | 56fdb7545363c5b1e2987b08fa9b271dd289ce66 (patch) | |
tree | 6e300097aa2ef4a8957ad6fb3112d54f4301ad30 /Lib | |
parent | 4a8cf3c50b58c48ae5f08e2fad4e99a207d8553e (diff) | |
download | cpython-56fdb7545363c5b1e2987b08fa9b271dd289ce66.zip cpython-56fdb7545363c5b1e2987b08fa9b271dd289ce66.tar.gz cpython-56fdb7545363c5b1e2987b08fa9b271dd289ce66.tar.bz2 |
Merged revisions 80920 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r80920 | michael.foord | 2010-05-07 17:52:05 +0200 (Fri, 07 May 2010) | 1 line
Adding tests for unittest command line handling of buffer, catchbreak and failfast.
........
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/unittest/test/test_program.py | 164 | ||||
-rw-r--r-- | Lib/unittest/test/test_runner.py | 46 |
2 files changed, 210 insertions, 0 deletions
diff --git a/Lib/unittest/test/test_program.py b/Lib/unittest/test/test_program.py index 752a066..f8e2804 100644 --- a/Lib/unittest/test/test_program.py +++ b/Lib/unittest/test/test_program.py @@ -1,6 +1,7 @@ import io import os +import sys import unittest @@ -89,3 +90,166 @@ class Test_TestProgram(unittest.TestCase): argv=["foobar"], testRunner=unittest.TextTestRunner(stream=io.StringIO()), testLoader=self.FooBarLoader()) + + +class InitialisableProgram(unittest.TestProgram): + exit = False + result = None + verbosity = 1 + defaultTest = None + testRunner = None + testLoader = unittest.defaultTestLoader + progName = 'test' + test = 'test' + def __init__(self, *args): + pass + +RESULT = object() + +class FakeRunner(object): + initArgs = None + test = None + raiseError = False + + def __init__(self, **kwargs): + FakeRunner.initArgs = kwargs + if FakeRunner.raiseError: + FakeRunner.raiseError = False + raise TypeError + + def run(self, test): + FakeRunner.test = test + return RESULT + +class TestCommandLineArgs(unittest.TestCase): + + def setUp(self): + self.program = InitialisableProgram() + self.program.createTests = lambda: None + FakeRunner.initArgs = None + FakeRunner.test = None + FakeRunner.raiseError = False + + def testHelpAndUnknown(self): + program = self.program + def usageExit(msg=None): + program.msg = msg + program.exit = True + program.usageExit = usageExit + + for opt in '-h', '-H', '--help': + program.exit = False + program.parseArgs([None, opt]) + self.assertTrue(program.exit) + self.assertIsNone(program.msg) + + program.parseArgs([None, '-$']) + self.assertTrue(program.exit) + self.assertIsNotNone(program.msg) + + def testVerbosity(self): + program = self.program + + for opt in '-q', '--quiet': + program.verbosity = 1 + program.parseArgs([None, opt]) + self.assertEqual(program.verbosity, 0) + + for opt in '-v', '--verbose': + program.verbosity = 1 + program.parseArgs([None, opt]) + self.assertEqual(program.verbosity, 2) + + def testBufferCatchFailfast(self): + program = self.program + for arg, attr in (('buffer', 'buffer'), ('failfast', 'failfast'), + ('catch', 'catchbreak')): + if attr == 'catch' and not hasInstallHandler: + continue + + short_opt = '-%s' % arg[0] + long_opt = '--%s' % arg + for opt in short_opt, long_opt: + setattr(program, attr, None) + + program.parseArgs([None, opt]) + self.assertTrue(getattr(program, attr)) + + for opt in short_opt, long_opt: + not_none = object() + setattr(program, attr, not_none) + + program.parseArgs([None, opt]) + self.assertEqual(getattr(program, attr), not_none) + + def testRunTestsRunnerClass(self): + program = self.program + + program.testRunner = FakeRunner + program.verbosity = 'verbosity' + program.failfast = 'failfast' + program.buffer = 'buffer' + + program.runTests() + + self.assertEqual(FakeRunner.initArgs, {'verbosity': 'verbosity', + 'failfast': 'failfast', + 'buffer': 'buffer'}) + self.assertEqual(FakeRunner.test, 'test') + self.assertIs(program.result, RESULT) + + def testRunTestsRunnerInstance(self): + program = self.program + + program.testRunner = FakeRunner() + FakeRunner.initArgs = None + + program.runTests() + + # A new FakeRunner should not have been instantiated + self.assertIsNone(FakeRunner.initArgs) + + self.assertEqual(FakeRunner.test, 'test') + self.assertIs(program.result, RESULT) + + def testRunTestsOldRunnerClass(self): + program = self.program + + FakeRunner.raiseError = True + program.testRunner = FakeRunner + program.verbosity = 'verbosity' + program.failfast = 'failfast' + program.buffer = 'buffer' + program.test = 'test' + + program.runTests() + + # If initialising raises a type error it should be retried + # without the new keyword arguments + self.assertEqual(FakeRunner.initArgs, {}) + self.assertEqual(FakeRunner.test, 'test') + self.assertIs(program.result, RESULT) + + def testCatchBreakInstallsHandler(self): + module = sys.modules['unittest.main'] + original = module.installHandler + def restore(): + module.installHandler = original + self.addCleanup(restore) + + self.installed = False + def fakeInstallHandler(): + self.installed = True + module.installHandler = fakeInstallHandler + + program = self.program + program.catchbreak = True + + program.testRunner = FakeRunner + + program.runTests() + self.assertTrue(self.installed) + + +if __name__ == '__main__': + unittest.main() diff --git a/Lib/unittest/test/test_runner.py b/Lib/unittest/test/test_runner.py index 4486e68..a7bd1a3 100644 --- a/Lib/unittest/test/test_runner.py +++ b/Lib/unittest/test/test_runner.py @@ -114,6 +114,52 @@ class TestCleanUp(unittest.TestCase): class Test_TextTestRunner(unittest.TestCase): """Tests for TextTestRunner.""" + def test_init(self): + runner = unittest.TextTestRunner() + self.assertFalse(runner.failfast) + self.assertFalse(runner.buffer) + self.assertEqual(runner.verbosity, 1) + self.assertTrue(runner.descriptions) + self.assertEqual(runner.resultclass, unittest.TextTestResult) + + + def testBufferAndFailfast(self): + class Test(unittest.TestCase): + def testFoo(self): + pass + result = unittest.TestResult() + runner = unittest.TextTestRunner(stream=io.StringIO(), failfast=True, + buffer=True) + # Use our result object + runner._makeResult = lambda: result + runner.run(Test('testFoo')) + + self.assertTrue(result.failfast) + self.assertTrue(result.buffer) + + def testRunnerRegistersResult(self): + class Test(unittest.TestCase): + def testFoo(self): + pass + originalRegisterResult = unittest.runner.registerResult + def cleanup(): + unittest.runner.registerResult = originalRegisterResult + self.addCleanup(cleanup) + + result = unittest.TestResult() + runner = unittest.TextTestRunner(stream=io.StringIO()) + # Use our result object + runner._makeResult = lambda: result + + self.wasRegistered = 0 + def fakeRegisterResult(thisResult): + self.wasRegistered += 1 + self.assertEqual(thisResult, result) + unittest.runner.registerResult = fakeRegisterResult + + runner.run(unittest.TestSuite()) + self.assertEqual(self.wasRegistered, 1) + def test_works_with_result_without_startTestRun_stopTestRun(self): class OldTextResult(ResultWithNoStartTestRunStopTestRun): separator2 = '' |