Skip to content

Commit c587089

Browse files
committed
Fix TestReport.longreprtext when TestReport.longrepr is not a string
Fix #7559
1 parent df09a31 commit c587089

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

changelog/7559.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix regression in plugins using ``TestReport.longreprtext`` (such as ``pytest-html``) when ``TestReport.longrepr`` is not a string.

src/_pytest/reports.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,10 @@ def toterminal(self, out: TerminalWriter) -> None:
8282
longrepr.toterminal(out)
8383
else:
8484
try:
85-
out.line(longrepr)
85+
s = str(longrepr)
8686
except UnicodeEncodeError:
87-
out.line("<unprintable longrepr>")
87+
s = "<unprintable longrepr>"
88+
out.line(s)
8889

8990
def get_sections(self, prefix: str) -> Iterator[Tuple[str, str]]:
9091
for name, content in self.sections:

testing/test_runner.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,19 @@ def test_func():
951951
rep = reports[1]
952952
assert rep.longreprtext == ""
953953

954+
def test_longreprtext_skip(self, testdir) -> None:
955+
"""TestReport.longreprtext can handle non-str ``longrepr`` attributes (#7559)"""
956+
reports = testdir.runitem(
957+
"""
958+
import pytest
959+
def test_func():
960+
pytest.skip()
961+
"""
962+
)
963+
_, call_rep, _ = reports
964+
assert isinstance(call_rep.longrepr, tuple)
965+
assert "Skipped" in call_rep.longreprtext
966+
954967
def test_longreprtext_failure(self, testdir) -> None:
955968
reports = testdir.runitem(
956969
"""

0 commit comments

Comments
 (0)