8000 bpo-24959: fix unittest.assertRaises bug where traceback entries are dropped from chained exceptions by iritkatriel · Pull Request #23688 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-24959: fix unittest.assertRaises bug where traceback entries are dropped from chained exceptions #23688

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Mar 8, 2022
22 changes: 14 additions & 8 deletions Lib/unittest/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,10 @@ def _exc_info_to_string(self, err, test):

if exctype is test.failureException:
# Skip assert*() traceback levels
length = self._count_relevant_tb_levels(tb)
else:
length = None
self._remove_unittest_tb_frames(tb)
tb_e = traceback.TracebackException(
exctype, value, tb,
limit=length, capture_locals=self.tb_locals, compact=True)
capture_locals=self.tb_locals, compact=True)
msgLines = list(tb_e.format())

if self.buffer:
Expand All @@ -204,12 +202,20 @@ def _exc_info_to_string(self, err, test):
def _is_relevant_tb_level(self, tb):
return '__unittest' in tb.tb_frame.f_globals

def _count_relevant_tb_levels(self, tb):
length = 0
def _remove_unittest_tb_frames( 8000 self, tb):
'''Truncates usercode tb at the first unittest frame.

If the first frame of the traceback is in user code,
the prefix up to the first unittest frame is returned.
If the first frame is already in the unittest module,
the traceback is not modified.
'''
prev = None
while tb and not self._is_relevant_tb_level(tb):
length += 1
prev = tb
tb = tb.tb_next
return length
if prev is not None:
prev.tb_next = None

def __repr__(self):
return ("<%s run=%i errors=%i failures=%i>" %
Expand Down
26 changes: 26 additions & 0 deletions Lib/unittest/test/test_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,32 @@ 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])

# "addError(test, err)"
# ...
# "Called when the test case test raises an unexpected exception err
Expand Down
0