This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: unittest filters out too many assertion stack frames from context/cause chains
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.9
process
Status: closed Resolution: duplicate
Dependencies: Superseder: unittest swallows part of stack trace when raising AssertionError in a TestCase
View: 24959
Assigned To: Nosy List: iritkatriel, peter.hawkins
Priority: normal Keywords:

Created on 2021-06-01 19:42 by peter.hawkins, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (2)
msg394865 - (view) Author: Peter Hawkins (peter.hawkins) Date: 2021-06-01 19:42
Example repro:
```
import unittest


def d():
  assert 2 == 3

def c():
  d()

def b():
  c()

def a():
  try:
    b()
  except Exception as e:
    assert 1 == 2


class MyTest(unittest.TestCase):

  def testException(self):
    a()


if __name__ == '__main__':
  unittest.main()
```

Example output from Python 3.9.0:
```
$ python atest.py
F
======================================================================
FAIL: testException (__main__.MyTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/private/tmp/atest.py", line 15, in a
    b()
  File "/private/tmp/atest.py", line 11, in b
    c()
AssertionError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/private/tmp/atest.py", line 23, in testException
    a()
  File "/private/tmp/atest.py", line 17, in a
    assert 1 == 2
AssertionError

----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (failures=1)
```

Too many frames have been filtered out of the `__context__` exception, including a number of relevant frames for `c` and `d`. I believe this happens because unittest sets a `limit` here based on the contents of the main traceback:
https://github.com/python/cpython/blob/39dd141a4ba68bbb38fd00a65cdcff711acdafb5/Lib/unittest/result.py#L182

but that limit applies recursively to the `__context__` and `__cause__` chains, when the intent of the limit was presumably only to filter the main exception.
msg394910 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-06-02 14:06
Agreed.
History
Date User Action Args
2022-04-11 14:59:46adminsetgithub: 88446
2021-06-02 14:06:41iritkatrielsetstatus: open -> closed

superseder: unittest swallows part of stack trace when raising AssertionError in a TestCase

nosy: + iritkatriel
messages: + msg394910
resolution: duplicate
stage: resolved
2021-06-01 19:52:00peter.hawkinssettype: behavior
components: + Library (Lib)
2021-06-01 19:42:48peter.hawkinscreate