175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chenfrom cStringIO import StringIO 275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chenimport sys 475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chenimport unittest2 575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny ChenhasInstallHandler = hasattr(unittest2, 'installHandler') 775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chenclass Test_TestProgram(unittest2.TestCase): 975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 1075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # Horrible white box test 1175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def testNoExit(self): 1275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen result = object() 1375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen test = object() 1475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 1575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class FakeRunner(object): 1675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def run(self, test): 1775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.test = test 1875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen return result 1975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 2075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen runner = FakeRunner() 2175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 2275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen oldParseArgs = unittest2.TestProgram.parseArgs 2375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def restoreParseArgs(): 2475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen unittest2.TestProgram.parseArgs = oldParseArgs 2575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen unittest2.TestProgram.parseArgs = lambda *args: None 2675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.addCleanup(restoreParseArgs) 2775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 2875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def removeTest(): 2975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen del unittest2.TestProgram.test 3075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen unittest2.TestProgram.test = test 3175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.addCleanup(removeTest) 3275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 3375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen program = unittest2.TestProgram(testRunner=runner, exit=False, verbosity=2) 3475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 3575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(program.result, result) 3675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(runner.test, test) 3775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(program.verbosity, 2) 3875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 3975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class FooBar(unittest2.TestCase): 4075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def testPass(self): 4175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen assert True 4275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def testFail(self): 4375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen assert False 4475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 4575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class FooBarLoader(unittest2.TestLoader): 4675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen """Test loader that returns a suite containing FooBar.""" 4775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def loadTestsFromModule(self, module): 4875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen return self.suiteClass( 4975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen [self.loadTestsFromTestCase(Test_TestProgram.FooBar)]) 5075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 5175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 5275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_NonExit(self): 5375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen program = unittest2.main(exit=False, 5475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen argv=["foobar"], 5575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen testRunner=unittest2.TextTestRunner(stream=StringIO()), 5675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen testLoader=self.FooBarLoader()) 5775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertTrue(hasattr(program, 'result')) 5875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 5975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 6075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_Exit(self): 6175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises( 6275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen SystemExit, 6375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen unittest2.main, 6475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen argv=["foobar"], 6575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen testRunner=unittest2.TextTestRunner(stream=StringIO()), 6675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen exit=True, 6775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen testLoader=self.FooBarLoader()) 6875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 6975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 7075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_ExitAsDefault(self): 7175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises( 7275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen SystemExit, 7375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen unittest2.main, 7475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen argv=["foobar"], 7575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen testRunner=unittest2.TextTestRunner(stream=StringIO()), 7675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen testLoader=self.FooBarLoader()) 7775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 7875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 7975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chenclass InitialisableProgram(unittest2.TestProgram): 8075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen exit = False 8175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen result = None 8275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen verbosity = 1 8375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen defaultTest = None 8475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen testRunner = None 8575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen testLoader = unittest2.defaultTestLoader 8675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen progName = 'test' 8775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen test = 'test' 8875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def __init__(self, *args): 8975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen pass 9075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 9175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny ChenRESULT = object() 9275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 9375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chenclass FakeRunner(object): 9475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen initArgs = None 9575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen test = None 9675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen raiseError = False 9775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 9875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def __init__(self, **kwargs): 9975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen FakeRunner.initArgs = kwargs 10075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen if FakeRunner.raiseError: 10175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen FakeRunner.raiseError = False 10275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen raise TypeError 10375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 10475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def run(self, test): 10575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen FakeRunner.test = test 10675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen return RESULT 10775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 10875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chenclass TestCommandLineArgs(unittest2.TestCase): 10975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 11075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def setUp(self): 11175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.program = InitialisableProgram() 11275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.program.createTests = lambda: None 11375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen FakeRunner.initArgs = None 11475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen FakeRunner.test = None 11575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen FakeRunner.raiseError = False 11675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 11775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def testHelpAndUnknown(self): 11875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen program = self.program 11975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def usageExit(msg=None): 12075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen program.msg = msg 12175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen program.exit = True 12275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen program.usageExit = usageExit 12375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 12475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen for opt in '-h', '-H', '--help': 12575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen program.exit = False 12675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen program.parseArgs([None, opt]) 12775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertTrue(program.exit) 12875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertIsNone(program.msg) 12975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 13075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen program.parseArgs([None, '-$']) 13175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertTrue(program.exit) 13275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertIsNotNone(program.msg) 13375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 13475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def testVerbosity(self): 13575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen program = self.program 13675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 13775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen for opt in '-q', '--quiet': 13875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen program.verbosity = 1 13975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen program.parseArgs([None, opt]) 14075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(program.verbosity, 0) 14175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 14275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen for opt in '-v', '--verbose': 14375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen program.verbosity = 1 14475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen program.parseArgs([None, opt]) 14575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(program.verbosity, 2) 14675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 14775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def testBufferCatchFailfast(self): 14875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen program = self.program 14975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen for arg, attr in (('buffer', 'buffer'), ('failfast', 'failfast'), 15075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen ('catch', 'catchbreak')): 15175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen if attr == 'catch' and not hasInstallHandler: 15275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen continue 15375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 15475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen short_opt = '-%s' % arg[0] 15575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen long_opt = '--%s' % arg 15675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen for opt in short_opt, long_opt: 15775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen setattr(program, attr, None) 15875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 15975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen program.parseArgs([None, opt]) 16075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertTrue(getattr(program, attr)) 16175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 16275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen for opt in short_opt, long_opt: 16375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen not_none = object() 16475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen setattr(program, attr, not_none) 16575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 16675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen program.parseArgs([None, opt]) 16775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(getattr(program, attr), not_none) 16875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 16975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def testRunTestsRunnerClass(self): 17075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen program = self.program 17175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 17275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen program.testRunner = FakeRunner 17375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen program.verbosity = 'verbosity' 17475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen program.failfast = 'failfast' 17575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen program.buffer = 'buffer' 17675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 17775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen program.runTests() 17875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 17975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(FakeRunner.initArgs, {'verbosity': 'verbosity', 18075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 'failfast': 'failfast', 18175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 'buffer': 'buffer'}) 18275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(FakeRunner.test, 'test') 18375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertIs(program.result, RESULT) 18475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 18575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def testRunTestsRunnerInstance(self): 18675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen program = self.program 18775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 18875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen program.testRunner = FakeRunner() 18975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen FakeRunner.initArgs = None 19075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 19175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen program.runTests() 19275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 19375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # A new FakeRunner should not have been instantiated 19475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertIsNone(FakeRunner.initArgs) 19575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 19675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(FakeRunner.test, 'test') 19775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertIs(program.result, RESULT) 19875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 19975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def testRunTestsOldRunnerClass(self): 20075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen program = self.program 20175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 20275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen FakeRunner.raiseError = True 20375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen program.testRunner = FakeRunner 20475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen program.verbosity = 'verbosity' 20575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen program.failfast = 'failfast' 20675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen program.buffer = 'buffer' 20775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen program.test = 'test' 20875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 20975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen program.runTests() 21075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 21175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # If initialising raises a type error it should be retried 21275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # without the new keyword arguments 21375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(FakeRunner.initArgs, {}) 21475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(FakeRunner.test, 'test') 21575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertIs(program.result, RESULT) 21675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 21775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def testCatchBreakInstallsHandler(self): 21875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen module = sys.modules['unittest2.main'] 21975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen original = module.installHandler 22075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def restore(): 22175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen module.installHandler = original 22275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.addCleanup(restore) 22375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 22475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.installed = False 22575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def fakeInstallHandler(): 22675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.installed = True 22775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen module.installHandler = fakeInstallHandler 22875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 22975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen program = self.program 23075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen program.catchbreak = True 23175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 23275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen program.testRunner = FakeRunner 23375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 23475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen program.runTests() 23575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertTrue(self.installed) 23675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 23775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 23875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chenif __name__ == '__main__': 23975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen unittest2.main() 240