Message249323
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) |
|
Date |
User |
Action |
Args |
2015-08-29 16:15:14 | chris.jerdonek | set | recipients:
+ chris.jerdonek |
2015-08-29 16:15:14 | chris.jerdonek | set | messageid: <1440864914.24.0.799928607113.issue24959@psf.upfronthosting.co.za> |
2015-08-29 16:15:14 | chris.jerdonek | link | issue24959 messages |
2015-08-29 16:15:13 | chris.jerdonek | create | |
|