Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix TestReport.longreprtext when TestReport.longrepr is not a string #7561

Merged
merged 1 commit into from Jul 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/7559.bugfix.rst
@@ -0,0 +1 @@
Fix regression in plugins using ``TestReport.longreprtext`` (such as ``pytest-html``) when ``TestReport.longrepr`` is not a string.
5 changes: 3 additions & 2 deletions src/_pytest/reports.py
Expand Up @@ -82,9 +82,10 @@ def toterminal(self, out: TerminalWriter) -> None:
longrepr.toterminal(out)
else:
try:
out.line(longrepr)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bluetech do you know why mypy didn't catch this problem? TerminalWriter.line expects a str argument, but longrepr is declared as Any in the class definition.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a function which accepts str also accepts Any because it's gradually typed (there's options to turn this off in mypy, but getting to that ideal is very very difficult)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh I indeed:

Any

A static type checker will treat every type as being compatible with Any and Any as being compatible with every type.

Thanks! My thought was that a function declaring str would accuse Any as incompatible, but the above definition clearly states otherwise. 👍

s = str(longrepr)
except UnicodeEncodeError:
out.line("<unprintable longrepr>")
s = "<unprintable longrepr>"
out.line(s)

def get_sections(self, prefix: str) -> Iterator[Tuple[str, str]]:
for name, content in self.sections:
Expand Down
27 changes: 27 additions & 0 deletions testing/test_runner.py
Expand Up @@ -951,6 +951,33 @@ def test_func():
rep = reports[1]
assert rep.longreprtext == ""

def test_longreprtext_skip(self, testdir) -> None:
"""TestReport.longreprtext can handle non-str ``longrepr`` attributes (#7559)"""
reports = testdir.runitem(
"""
import pytest
def test_func():
pytest.skip()
"""
)
_, call_rep, _ = reports
assert isinstance(call_rep.longrepr, tuple)
assert "Skipped" in call_rep.longreprtext

def test_longreprtext_collect_skip(self, testdir) -> None:
"""CollectReport.longreprtext can handle non-str ``longrepr`` attributes (#7559)"""
testdir.makepyfile(
"""
import pytest
pytest.skip(allow_module_level=True)
"""
)
rec = testdir.inline_run()
calls = rec.getcalls("pytest_collectreport")
_, call = calls
assert isinstance(call.report.longrepr, tuple)
assert "Skipped" in call.report.longreprtext

def test_longreprtext_failure(self, testdir) -> None:
reports = testdir.runitem(
"""
Expand Down