[New-bugs-announce] [issue24959] unittest swallows part of stack trace using "raise from" with AssertionError

Chris Jerdonek report at bugs.python.org
Sat Aug 29 18:15:14 CEST 2015


New submission from Chris Jerdonek:

unittest swallows some lines of the stack trace when raising an AssertionError using the "raise from" syntax inside a TestCase.  This marks it harder to pinpoint the source of test failures.  It is also confusing to see a stack trace like this because the error doesn't originate where the stack trace says it originates.

To reproduce:

    import unittest

    def foo():
        raise Exception("foo")

    class Test(unittest.TestCase):

        def test_not_okay(self):
            try:
                foo()
            except Exception as exc:
                raise AssertionError("bar") from exc

        def test_okay1(self):
            try:
                foo()
            except Exception as exc:
                raise ValueError("bar") from exc

        def test_okay2(self):
            try:
                foo()
            except Exception as exc:
                raise Exception("bar") from exc

The result (observe how the display for "test_not_okay" differs from the other two):

    ======================================================================
    ERROR: test_okay1 (error.Test)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/Users/chris/dev/error.py", line 17, in test_okay1
        foo()
      File "/Users/chris/dev/error.py", line 5, in foo
        raise Exception("foo")
    Exception: foo

    The above exception was the direct cause of the following exception:

    Traceback (most recent call last):
      File "/Users/chris/dev/error.py", line 19, in test_okay1
        raise ValueError("bar") from exc
    ValueError: bar

    ======================================================================
    ERROR: test_okay2 (error.Test)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/Users/chris/dev/error.py", line 23, in test_okay2
        foo()
      File "/Users/chris/dev/error.py", line 5, in foo
        raise Exception("foo")
    Exception: foo

    The above exception was the direct cause of the following exception:

    Traceback (most recent call last):
      File "/Users/chris/dev/error.py", line 25, in test_okay2
        raise Exception("bar") from exc
    Exception: bar

    ======================================================================
    FAIL: test_not_okay (error.Test)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/Users/chris/dev/error.py", line 11, in test_not_okay
        foo()
    Exception: foo

    The above exception was the direct cause of the following exception:

    Traceback (most recent call last):
      File "/Users/chris/dev/error.py", line 13, in test_not_okay
        raise AssertionError("bar") from exc
    AssertionError: bar

    ----------------------------------------------------------------------
    Ran 3 tests in 0.001s

    FAILED (failures=1, errors=2)

----------
components: Library (Lib)
messages: 249323
nosy: chris.jerdonek
priority: normal
severity: normal
status: open
title: unittest swallows part of stack trace using "raise from" with AssertionError
type: behavior
versions: Python 3.4

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue24959>
_______________________________________


More information about the New-bugs-announce mailing list