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: Exception frames from unittest.TestCase.fail dependent on nesting
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.7
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: Matthew Roeschke, chris.jerdonek, ezio.melotti, iritkatriel, michael.foord, rbcollins
Priority: normal Keywords:

Created on 2019-07-30 00:57 by Matthew Roeschke, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg348708 - (view) Author: Matthew Roeschke (Matthew Roeschke) Date: 2019-07-30 00:57
With this toy example:

import unittest

def this_fails():
    a = 1 + None

class TestExample(unittest.TestCase):

    def test_this(self):
        try:
            this_fails()
        except Exception:
            self.fail('Fail')

if __name__ == '__main__':
    unittest.run()

I get the last frame for each chained exception:

Traceback (most recent call last):
  File "/Users/me/test.py", line 10, in test_this
    this_fails()
TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/me/test.py", line 12, in test_this
    self.fail('Fail')
AssertionError: Fail

But if the toy example contained a nested call, e.g.

    def helper(self):
        try:
            this_fails()
        except Exception:
            self.fail('Fail')

    def test_this(self):
        self.helper() 

I get the last 2 frames for each chained exception:

Traceback (most recent call last):
  File "/Users/me/test.py", line 10, in helper
    this_fails()
  File "/Users/me/test.py", line 4, in this_fails
    a = 1 + None
TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/me/test.py", line 15, in test_this
    self.helper()
  File "/Users/me/test.py", line 12, in helper
    self.fail('Fail')
AssertionError: Fail

Ideally, it would be great if the traceback went back to the root of the exception regardless.
msg390488 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-04-07 22:06
I think this is the same as issue42247.
msg390617 - (view) Author: Chris Jerdonek (chris.jerdonek) * (Python committer) Date: 2021-04-09 13:02
> I think this is the same as issue37712.

This issue was first reported as issue24959. It would be better to close the newer issues as duplicates of the first one, instead of keeping all the duplicates open. Otherwise, the history and discussion gets fragmented across multiple locations.
History
Date User Action Args
2022-04-11 14:59:18adminsetgithub: 81893
2021-04-09 13:26:49iritkatrielsetstatus: open -> closed
superseder: unittest swallows part of stack trace when raising AssertionError in a TestCase
resolution: duplicate
stage: resolved
2021-04-09 13:02:35chris.jerdoneksetnosy: + chris.jerdonek
messages: + msg390617
2021-04-07 22:06:34iritkatrielsetnosy: + iritkatriel
messages: + msg390488
2019-07-30 07:18:21xtreaksetnosy: + rbcollins, ezio.melotti, michael.foord
2019-07-30 00:57:04Matthew Roeschkecreate