Skip to content

Commit 36adf00

Browse files
committed
Fixed issue shadowing error when missing argument on teardown_method
When the method argument is missing on teardown_method, the traceback is 100% internal to pytest, which with default options get pruned. Then that traceback is empty, leading to a new exception as a traceback shall not be empty. This PR fixes that issue by pushing back the last stack on the traceback, when the stacktrace is empty after pruning. Then the output is still pruned, but gives meaningful information with the item where it failed on the stack. * fixes issue pytest-dev#1604 Signed-off-by: Guyzmo <[email protected]>
1 parent 577cce2 commit 36adf00

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,4 @@ Thomas Grainger
9191
Tom Viner
9292
Trevor Bekolay
9393
Wouter van Ackooy
94+
Bernard Pratz

CHANGELOG.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
* Text documents without any doctests no longer appear as "skipped".
77
Thanks `@graingert`_ for reporting and providing a full PR (`#1580`_).
88

9-
*
9+
* Fix internal error issue when `method` argument is missing for
10+
`teardown_method`. Fixes (`#1605`).
1011

1112
*
1213

_pytest/main.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,10 @@ def _repr_failure_py(self, excinfo, style=None):
392392
if self.config.option.fulltrace:
393393
style="long"
394394
else:
395+
tb = _pytest._code.Traceback([excinfo.traceback[-1]])
395396
self._prunetraceback(excinfo)
397+
if len(excinfo.traceback) == 0:
398+
excinfo.traceback = tb
396399
tbfilter = False # prunetraceback already does it
397400
if style == "auto":
398401
style = "long"

testing/test_runner.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,34 @@ def teardown_function(func):
228228
assert reps[5].nodeid.endswith("test_func")
229229
assert reps[5].failed
230230

231+
def test_exact_teardown_issue1206(self, testdir):
232+
rec = testdir.inline_runsource("""
233+
import pytest
234+
235+
class TestClass:
236+
def teardown_method(self):
237+
pass
238+
239+
def test_method(self):
240+
assert True
241+
""")
242+
reps = rec.getreports("pytest_runtest_logreport")
243+
print (reps)
244+
assert len(reps) == 3
245+
#
246+
assert reps[0].nodeid.endswith("test_method")
247+
assert reps[0].passed
248+
assert reps[0].when == 'setup'
249+
#
250+
assert reps[1].nodeid.endswith("test_method")
251+
assert reps[1].passed
252+
assert reps[1].when == 'call'
253+
#
254+
assert reps[2].nodeid.endswith("test_method")
255+
assert reps[2].failed
256+
assert reps[2].when == "teardown"
257+
assert 'TypeError: teardown_method() takes 1 positional argument but 2 were given' in reps[2].longrepr.reprcrash.message
258+
231259
def test_failure_in_setup_function_ignores_custom_repr(self, testdir):
232260
testdir.makepyfile(conftest="""
233261
import pytest

0 commit comments

Comments
 (0)