diff options
author | Michael Foord <fuzzyman@voidspace.org.uk> | 2010-12-19 03:19:47 (GMT) |
---|---|---|
committer | Michael Foord <fuzzyman@voidspace.org.uk> | 2010-12-19 03:19:47 (GMT) |
commit | b3468f79efa45c8adaf86c0b9b797b9b3d4c12a2 (patch) | |
tree | 14e9ae4dcb071ee7bf154026ecac93c3ea352c7a /Lib/unittest/test | |
parent | addc6f5a21d5d04c5bf895e8ea46f67de129d75c (diff) | |
download | cpython-b3468f79efa45c8adaf86c0b9b797b9b3d4c12a2.zip cpython-b3468f79efa45c8adaf86c0b9b797b9b3d4c12a2.tar.gz cpython-b3468f79efa45c8adaf86c0b9b797b9b3d4c12a2.tar.bz2 |
Issue 10611. Issue 9857. Improve the way exception handling, including test skipping, is done inside TestCase.run
Diffstat (limited to 'Lib/unittest/test')
-rw-r--r-- | Lib/unittest/test/test_case.py | 95 | ||||
-rw-r--r-- | Lib/unittest/test/test_functiontestcase.py | 8 | ||||
-rw-r--r-- | Lib/unittest/test/test_runner.py | 19 |
3 files changed, 99 insertions, 23 deletions
diff --git a/Lib/unittest/test/test_case.py b/Lib/unittest/test/test_case.py index a56baa1..3ad883d 100644 --- a/Lib/unittest/test/test_case.py +++ b/Lib/unittest/test/test_case.py @@ -177,8 +177,8 @@ class Test_TestCase(unittest.TestCase, TestEquality, TestHashing): super(Foo, self).test() raise RuntimeError('raised by Foo.test') - expected = ['startTest', 'setUp', 'test', 'addError', 'tearDown', - 'stopTest'] + expected = ['startTest', 'setUp', 'test', 'tearDown', + 'addError', 'stopTest'] Foo(events).run(result) self.assertEqual(events, expected) @@ -195,8 +195,8 @@ class Test_TestCase(unittest.TestCase, TestEquality, TestHashing): super(Foo, self).test() raise RuntimeError('raised by Foo.test') - expected = ['startTestRun', 'startTest', 'setUp', 'test', 'addError', - 'tearDown', 'stopTest', 'stopTestRun'] + expected = ['startTestRun', 'startTest', 'setUp', 'test', + 'tearDown', 'addError', 'stopTest', 'stopTestRun'] Foo(events).run() self.assertEqual(events, expected) @@ -216,8 +216,8 @@ class Test_TestCase(unittest.TestCase, TestEquality, TestHashing): super(Foo, self).test() self.fail('raised by Foo.test') - expected = ['startTest', 'setUp', 'test', 'addFailure', 'tearDown', - 'stopTest'] + expected = ['startTest', 'setUp', 'test', 'tearDown', + 'addFailure', 'stopTest'] Foo(events).run(result) self.assertEqual(events, expected) @@ -231,8 +231,8 @@ class Test_TestCase(unittest.TestCase, TestEquality, TestHashing): super(Foo, self).test() self.fail('raised by Foo.test') - expected = ['startTestRun', 'startTest', 'setUp', 'test', 'addFailure', - 'tearDown', 'stopTest', 'stopTestRun'] + expected = ['startTestRun', 'startTest', 'setUp', 'test', + 'tearDown', 'addFailure', 'stopTest', 'stopTestRun'] events = [] Foo(events).run() self.assertEqual(events, expected) @@ -1126,3 +1126,82 @@ test case # exercise the TestCase instance in a way that will invoke # the type equality lookup mechanism unpickled_test.assertEqual(set(), set()) + + def testKeyboardInterrupt(self): + def _raise(self=None): + raise KeyboardInterrupt + def nothing(self): + pass + + class Test1(unittest.TestCase): + test_something = _raise + + class Test2(unittest.TestCase): + setUp = _raise + test_something = nothing + + class Test3(unittest.TestCase): + test_something = nothing + tearDown = _raise + + class Test4(unittest.TestCase): + def test_something(self): + self.addCleanup(_raise) + + for klass in (Test1, Test2, Test3, Test4): + with self.assertRaises(KeyboardInterrupt): + klass('test_something').run() + + def testSkippingEverywhere(self): + def _skip(self=None): + raise unittest.SkipTest('some reason') + def nothing(self): + pass + + class Test1(unittest.TestCase): + test_something = _skip + + class Test2(unittest.TestCase): + setUp = _skip + test_something = nothing + + class Test3(unittest.TestCase): + test_something = nothing + tearDown = _skip + + class Test4(unittest.TestCase): + def test_something(self): + self.addCleanup(_skip) + + for klass in (Test1, Test2, Test3, Test4): + result = unittest.TestResult() + klass('test_something').run(result) + self.assertEqual(len(result.skipped), 1) + self.assertEqual(result.testsRun, 1) + + def testSystemExit(self): + def _raise(self=None): + raise SystemExit + def nothing(self): + pass + + class Test1(unittest.TestCase): + test_something = _raise + + class Test2(unittest.TestCase): + setUp = _raise + test_something = nothing + + class Test3(unittest.TestCase): + test_something = nothing + tearDown = _raise + + class Test4(unittest.TestCase): + def test_something(self): + self.addCleanup(_raise) + + for klass in (Test1, Test2, Test3, Test4): + result = unittest.TestResult() + klass('test_something').run(result) + self.assertEqual(len(result.errors), 1) + self.assertEqual(result.testsRun, 1) diff --git a/Lib/unittest/test/test_functiontestcase.py b/Lib/unittest/test/test_functiontestcase.py index ab46785..9ce5ee3 100644 --- a/Lib/unittest/test/test_functiontestcase.py +++ b/Lib/unittest/test/test_functiontestcase.py @@ -58,8 +58,8 @@ class Test_FunctionTestCase(unittest.TestCase): def tearDown(): events.append('tearDown') - expected = ['startTest', 'setUp', 'test', 'addError', 'tearDown', - 'stopTest'] + expected = ['startTest', 'setUp', 'test', 'tearDown', + 'addError', 'stopTest'] unittest.FunctionTestCase(test, setUp, tearDown).run(result) self.assertEqual(events, expected) @@ -84,8 +84,8 @@ class Test_FunctionTestCase(unittest.TestCase): def tearDown(): events.append('tearDown') - expected = ['startTest', 'setUp', 'test', 'addFailure', 'tearDown', - 'stopTest'] + expected = ['startTest', 'setUp', 'test', 'tearDown', + 'addFailure', 'stopTest'] unittest.FunctionTestCase(test, setUp, tearDown).run(result) self.assertEqual(events, expected) diff --git a/Lib/unittest/test/test_runner.py b/Lib/unittest/test/test_runner.py index 8f4aaaa..8f98a02 100644 --- a/Lib/unittest/test/test_runner.py +++ b/Lib/unittest/test/test_runner.py @@ -34,9 +34,7 @@ class TestCleanUp(unittest.TestCase): [(cleanup1, (1, 2, 3), dict(four='hello', five='goodbye')), (cleanup2, (), {})]) - result = test.doCleanups() - self.assertTrue(result) - + self.assertTrue(test.doCleanups()) self.assertEqual(cleanups, [(2, (), {}), (1, (1, 2, 3), dict(four='hello', five='goodbye'))]) def testCleanUpWithErrors(self): @@ -44,14 +42,12 @@ class TestCleanUp(unittest.TestCase): def testNothing(self): pass - class MockResult(object): + class MockOutcome(object): + success = True errors = [] - def addError(self, test, exc_info): - self.errors.append((test, exc_info)) - result = MockResult() test = TestableTest('testNothing') - test._resultForDoCleanups = result + test._outcomeForDoCleanups = MockOutcome exc1 = Exception('foo') exc2 = Exception('bar') @@ -65,10 +61,11 @@ class TestCleanUp(unittest.TestCase): test.addCleanup(cleanup2) self.assertFalse(test.doCleanups()) + self.assertFalse(MockOutcome.success) - (test1, (Type1, instance1, _)), (test2, (Type2, instance2, _)) = reversed(MockResult.errors) - self.assertEqual((test1, Type1, instance1), (test, Exception, exc1)) - self.assertEqual((test2, Type2, instance2), (test, Exception, exc2)) + (Type1, instance1, _), (Type2, instance2, _) = reversed(MockOutcome.errors) + self.assertEqual((Type1, instance1), (Exception, exc1)) + self.assertEqual((Type2, instance2), (Exception, exc2)) def testCleanupInRun(self): blowUp = False |