Skip to content

Commit d3267bc

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

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

changelog/7559.bugfix.rst

+1
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

+3-2
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

+27
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,33 @@ 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+
967+
def test_longreprtext_collect_skip(self, testdir) -> None:
968+
"""CollectReport.longreprtext can handle non-str ``longrepr`` attributes (#7559)"""
969+
testdir.makepyfile(
970+
"""
971+
import pytest
972+
pytest.skip(allow_module_level=True)
973+
"""
974+
)
975+
rec = testdir.inline_run()
976+
calls = rec.getcalls("pytest_collectreport")
977+
_, call = calls
978+
assert isinstance(call.report.longrepr, tuple)
979+
assert "Skipped" in call.report.longreprtext
980+
954981
def test_longreprtext_failure(self, testdir) -> None:
955982
reports = testdir.runitem(
956983
"""

0 commit comments

Comments
 (0)