diff options
author | Irit Katriel <1055913+iritkatriel@users.noreply.github.com> | 2022-03-08 21:43:49 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-08 21:43:49 (GMT) |
commit | 88b7d86a73da9388aa65c96401c2984c8c16f8db (patch) | |
tree | 3dd2dc7d23ffbef10f514821c92ebccd191dd7e9 /Lib/unittest/test | |
parent | da80d6b2f3beff519cb1457d5e055168c89f7224 (diff) | |
download | cpython-88b7d86a73da9388aa65c96401c2984c8c16f8db.zip cpython-88b7d86a73da9388aa65c96401c2984c8c16f8db.tar.gz cpython-88b7d86a73da9388aa65c96401c2984c8c16f8db.tar.bz2 |
bpo-24959: fix unittest.assertRaises bug where traceback entries are dropped from chained exceptions (GH-23688)
Diffstat (limited to 'Lib/unittest/test')
-rw-r--r-- | Lib/unittest/test/test_result.py | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/Lib/unittest/test/test_result.py b/Lib/unittest/test/test_result.py index 224a784..c616f28 100644 --- a/Lib/unittest/test/test_result.py +++ b/Lib/unittest/test/test_result.py @@ -220,6 +220,61 @@ class Test_TestResult(unittest.TestCase): self.assertIs(test_case, test) self.assertIsInstance(formatted_exc, str) + def test_addFailure_filter_traceback_frames(self): + class Foo(unittest.TestCase): + def test_1(self): + pass + + test = Foo('test_1') + def get_exc_info(): + try: + test.fail("foo") + except: + return sys.exc_info() + + exc_info_tuple = get_exc_info() + + full_exc = traceback.format_exception(*exc_info_tuple) + + result = unittest.TestResult() + result.startTest(test) + result.addFailure(test, exc_info_tuple) + result.stopTest(test) + + formatted_exc = result.failures[0][1] + dropped = [l for l in full_exc if l not in formatted_exc] + self.assertEqual(len(dropped), 1) + self.assertIn("raise self.failureException(msg)", dropped[0]) + + def test_addFailure_filter_traceback_frames_context(self): + class Foo(unittest.TestCase): + def test_1(self): + pass + + test = Foo('test_1') + def get_exc_info(): + try: + try: + test.fail("foo") + except: + raise ValueError(42) + except: + return sys.exc_info() + + exc_info_tuple = get_exc_info() + + full_exc = traceback.format_exception(*exc_info_tuple) + + result = unittest.TestResult() + result.startTest(test) + result.addFailure(test, exc_info_tuple) + result.stopTest(test) + + formatted_exc = result.failures[0][1] + dropped = [l for l in full_exc if l not in formatted_exc] + self.assertEqual(len(dropped), 1) + self.assertIn("raise self.failureException(msg)", dropped[0]) + # "addError(test, err)" # ... # "Called when the test case test raises an unexpected exception err |