Skip to content

Commit 02188e3

Browse files
authored
Merge pull request #4987 from blueyed/collect-tbstyle-repr_failure
CollectError.repr_failure: honor explicit tbstyle option
2 parents 49f36bb + b18df93 commit 02188e3

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

changelog/4987.trivial.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
``Collector.repr_failure`` respects ``--tbstyle``, but only defaults to ``short`` now (with ``auto``).

src/_pytest/nodes.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,14 @@ def repr_failure(self, excinfo):
325325
if excinfo.errisinstance(self.CollectError):
326326
exc = excinfo.value
327327
return str(exc.args[0])
328-
return self._repr_failure_py(excinfo, style="short")
328+
329+
# Respect explicit tbstyle option, but default to "short"
330+
# (None._repr_failure_py defaults to "long" without "fulltrace" option).
331+
tbstyle = self.config.getoption("tbstyle")
332+
if tbstyle == "auto":
333+
tbstyle = "short"
334+
335+
return self._repr_failure_py(excinfo, style=tbstyle)
329336

330337
def _prunetraceback(self, excinfo):
331338
if hasattr(self, "fspath"):

testing/test_collection.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import pytest
1313
from _pytest.main import _in_venv
14+
from _pytest.main import EXIT_INTERRUPTED
1415
from _pytest.main import EXIT_NOTESTSCOLLECTED
1516
from _pytest.main import Session
1617

@@ -1234,3 +1235,20 @@ def test_collect_sub_with_symlinks(use_pkg, testdir):
12341235
"*2 passed in*",
12351236
]
12361237
)
1238+
1239+
1240+
def test_collector_respects_tbstyle(testdir):
1241+
p1 = testdir.makepyfile("assert 0")
1242+
result = testdir.runpytest(p1, "--tb=native")
1243+
assert result.ret == EXIT_INTERRUPTED
1244+
result.stdout.fnmatch_lines(
1245+
[
1246+
"*_ ERROR collecting test_collector_respects_tbstyle.py _*",
1247+
"Traceback (most recent call last):",
1248+
' File "*/test_collector_respects_tbstyle.py", line 1, in <module>',
1249+
" assert 0",
1250+
"AssertionError: assert 0",
1251+
"*! Interrupted: 1 errors during collection !*",
1252+
"*= 1 error in *",
1253+
]
1254+
)

0 commit comments

Comments
 (0)