Skip to content

Commit 41f57ef

Browse files
authored
Fix pytrace=False and --tb=line reports None (#10905)
Closes #10831. This fixes a small bug where running tests that contained `pytest.fail(pytrace=False)` with the `--tb=line` flag set results in an output of "None" in the Failures section of the output, and adds a test to ensure the behavior is correct.
1 parent 4eca606 commit 41f57ef

File tree

4 files changed

+17
-4
lines changed

4 files changed

+17
-4
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Ahn Ki-Wook
1313
Akiomi Kamakura
1414
Alan Velasco
1515
Alessio Izzo
16+
Alex Jones
1617
Alexander Johnson
1718
Alexander King
1819
Alexei Kozlenok

changelog/10831.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Terminal Reporting: Fixed bug when running in ``--tb=line`` mode where ``pytest.fail(pytrace=False)`` tests report ``None``.

src/_pytest/_code/code.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ def getrepr(
647647
Ignored if ``style=="native"``.
648648
649649
:param str style:
650-
long|short|no|native|value traceback style.
650+
long|short|line|no|native|value traceback style.
651651
652652
:param bool abspath:
653653
If paths should be changed to absolute or left unchanged.
@@ -977,9 +977,7 @@ def repr_excinfo(
977977
)
978978
else:
979979
reprtraceback = self.repr_traceback(excinfo_)
980-
reprcrash: Optional[ReprFileLocation] = (
981-
excinfo_._getreprcrash() if self.style != "value" else None
982-
)
980+
reprcrash = excinfo_._getreprcrash()
983981
else:
984982
# Fallback to native repr if the exception doesn't have a traceback:
985983
# ExceptionInfo objects require a full traceback to work.

testing/test_terminal.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1539,6 +1539,19 @@ def test_func2():
15391539
s = result.stdout.str()
15401540
assert "def test_func2" not in s
15411541

1542+
def test_tb_crashline_pytrace_false(self, pytester: Pytester, option) -> None:
1543+
p = pytester.makepyfile(
1544+
"""
1545+
import pytest
1546+
def test_func1():
1547+
pytest.fail('test_func1', pytrace=False)
1548+
"""
1549+
)
1550+
result = pytester.runpytest("--tb=line")
1551+
result.stdout.str()
1552+
bn = p.name
1553+
result.stdout.fnmatch_lines(["*%s:3: Failed: test_func1" % bn])
1554+
15421555
def test_pytest_report_header(self, pytester: Pytester, option) -> None:
15431556
pytester.makeconftest(
15441557
"""

0 commit comments

Comments
 (0)