bpo-24959: fix unittest.assertRaises bug where traceback entries are dropped from chained exceptions (GH-23688) (GH-31776)

(cherry picked from commit 88b7d86a73)
This commit is contained in:
Irit Katriel 2022-03-08 23:00:45 +00:00 committed by GitHub
parent 20e88f78a3
commit f3ea249569
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 95 additions and 14 deletions

View file

@ -221,6 +221,61 @@ def test_1(self):
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