From de44293d59d6aea0fb470d14382f676fd0acec89 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Sat, 23 Mar 2019 15:18:22 +0100 Subject: [PATCH 1/2] CollectError.repr_failure: honor explicit tbstyle option --- src/_pytest/nodes.py | 9 ++++++++- testing/test_collection.py | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/_pytest/nodes.py b/src/_pytest/nodes.py index ce02e70cd..fcaf59312 100644 --- a/src/_pytest/nodes.py +++ b/src/_pytest/nodes.py @@ -325,7 +325,14 @@ class Collector(Node): if excinfo.errisinstance(self.CollectError): exc = excinfo.value return str(exc.args[0]) - return self._repr_failure_py(excinfo, style="short") + + # Respect explicit tbstyle option, but default to "short" + # (None._repr_failure_py defaults to "long" without "fulltrace" option). + tbstyle = self.config.getoption("tbstyle") + if tbstyle == "auto": + tbstyle = "short" + + return self._repr_failure_py(excinfo, style=tbstyle) def _prunetraceback(self, excinfo): if hasattr(self, "fspath"): diff --git a/testing/test_collection.py b/testing/test_collection.py index 37f7ad89c..b6e0ce4ac 100644 --- a/testing/test_collection.py +++ b/testing/test_collection.py @@ -11,6 +11,7 @@ import py import pytest from _pytest.main import _in_venv +from _pytest.main import EXIT_INTERRUPTED from _pytest.main import EXIT_NOTESTSCOLLECTED from _pytest.main import Session @@ -1234,3 +1235,20 @@ def test_collect_sub_with_symlinks(use_pkg, testdir): "*2 passed in*", ] ) + + +def test_collector_respects_tbstyle(testdir): + p1 = testdir.makepyfile("assert 0") + result = testdir.runpytest(p1, "--tb=native") + assert result.ret == EXIT_INTERRUPTED + result.stdout.fnmatch_lines( + [ + "*_ ERROR collecting test_collector_respects_tbstyle.py _*", + "Traceback (most recent call last):", + ' File "*/test_collector_respects_tbstyle.py", line 1, in ', + " assert 0", + "AssertionError: assert 0", + "*! Interrupted: 1 errors during collection !*", + "*= 1 error in *", + ] + ) From b18df936eaf4467f20386946bfacc07a4a3256bc Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Tue, 26 Mar 2019 10:06:11 +0100 Subject: [PATCH 2/2] changelog --- changelog/4987.trivial.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog/4987.trivial.rst diff --git a/changelog/4987.trivial.rst b/changelog/4987.trivial.rst new file mode 100644 index 000000000..eb79b742a --- /dev/null +++ b/changelog/4987.trivial.rst @@ -0,0 +1 @@ +``Collector.repr_failure`` respects ``--tbstyle``, but only defaults to ``short`` now (with ``auto``).