From 36adf0064add6b7626fbddecbfac47e491bac3c3 Mon Sep 17 00:00:00 2001 From: Guyzmo Date: Sat, 11 Jun 2016 18:04:49 +0200 Subject: [PATCH] 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 #1604 Signed-off-by: Guyzmo --- AUTHORS | 1 + CHANGELOG.rst | 3 ++- _pytest/main.py | 3 +++ testing/test_runner.py | 28 ++++++++++++++++++++++++++++ 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/AUTHORS b/AUTHORS index d740fee5ce3..28cce969d63 100644 --- a/AUTHORS +++ b/AUTHORS @@ -91,3 +91,4 @@ Thomas Grainger Tom Viner Trevor Bekolay Wouter van Ackooy +Bernard Pratz diff --git a/CHANGELOG.rst b/CHANGELOG.rst index adc6993ebb8..30a966780bc 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -6,7 +6,8 @@ * Text documents without any doctests no longer appear as "skipped". Thanks `@graingert`_ for reporting and providing a full PR (`#1580`_). -* +* Fix internal error issue when `method` argument is missing for + `teardown_method`. Fixes (`#1605`). * diff --git a/_pytest/main.py b/_pytest/main.py index f608a7ecd5c..4a6c0877557 100644 --- a/_pytest/main.py +++ b/_pytest/main.py @@ -392,7 +392,10 @@ def _repr_failure_py(self, excinfo, style=None): if self.config.option.fulltrace: style="long" else: + tb = _pytest._code.Traceback([excinfo.traceback[-1]]) self._prunetraceback(excinfo) + if len(excinfo.traceback) == 0: + excinfo.traceback = tb tbfilter = False # prunetraceback already does it if style == "auto": style = "long" diff --git a/testing/test_runner.py b/testing/test_runner.py index 4421c5d0d29..676963cf9e8 100644 --- a/testing/test_runner.py +++ b/testing/test_runner.py @@ -228,6 +228,34 @@ def teardown_function(func): assert reps[5].nodeid.endswith("test_func") assert reps[5].failed + def test_exact_teardown_issue1206(self, testdir): + rec = testdir.inline_runsource(""" + import pytest + + class TestClass: + def teardown_method(self): + pass + + def test_method(self): + assert True + """) + reps = rec.getreports("pytest_runtest_logreport") + print (reps) + assert len(reps) == 3 + # + assert reps[0].nodeid.endswith("test_method") + assert reps[0].passed + assert reps[0].when == 'setup' + # + assert reps[1].nodeid.endswith("test_method") + assert reps[1].passed + assert reps[1].when == 'call' + # + assert reps[2].nodeid.endswith("test_method") + assert reps[2].failed + assert reps[2].when == "teardown" + assert 'TypeError: teardown_method() takes 1 positional argument but 2 were given' in reps[2].longrepr.reprcrash.message + def test_failure_in_setup_function_ignores_custom_repr(self, testdir): testdir.makepyfile(conftest=""" import pytest