Skip to content

Commit ba407b5

Browse files
committed
Clear sys.last_* attributes before running an item
Otherwise we will keep the last failed exception around forever Related to pytest-dev#2798
1 parent ad0b433 commit ba407b5

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

_pytest/runner.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ def pytest_runtest_setup(item):
105105

106106
def pytest_runtest_call(item):
107107
_update_current_test_var(item, 'call')
108+
sys.last_type, sys.last_value, sys.last_traceback = (None, None, None)
108109
try:
109110
item.runtest()
110111
except Exception:
@@ -114,7 +115,7 @@ def pytest_runtest_call(item):
114115
sys.last_type = type
115116
sys.last_value = value
116117
sys.last_traceback = tb
117-
del tb # Get rid of it in this namespace
118+
del type, value, tb # Get rid of these in this frame
118119
raise
119120

120121

testing/test_runner.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -719,25 +719,34 @@ def test_fix(foo):
719719
result.stdout.fnmatch_lines(["*test_fix*", "*fixture*'missing'*not found*"])
720720

721721

722-
def test_store_except_info_on_eror():
722+
def test_store_except_info_on_error():
723723
""" Test that upon test failure, the exception info is stored on
724724
sys.last_traceback and friends.
725725
"""
726-
# Simulate item that raises a specific exception
727-
class ItemThatRaises(object):
726+
# Simulate item that might raise a specific exception, depending on `raise_error` class var
727+
class ItemMightRaise(object):
728728
nodeid = 'item_that_raises'
729+
raise_error = True
729730

730731
def runtest(self):
731-
raise IndexError('TEST')
732+
if self.raise_error:
733+
raise IndexError('TEST')
732734
try:
733-
runner.pytest_runtest_call(ItemThatRaises())
735+
runner.pytest_runtest_call(ItemMightRaise())
734736
except IndexError:
735737
pass
736738
# Check that exception info is stored on sys
737739
assert sys.last_type is IndexError
738740
assert sys.last_value.args[0] == 'TEST'
739741
assert sys.last_traceback
740742

743+
# The next run should clear the exception info stored by the previous run
744+
ItemMightRaise.raise_error = False
745+
runner.pytest_runtest_call(ItemMightRaise())
746+
assert sys.last_type is None
747+
assert sys.last_value is None
748+
assert sys.last_traceback is None
749+
741750

742751
def test_current_test_env_var(testdir, monkeypatch):
743752
pytest_current_test_vars = []

0 commit comments

Comments
 (0)