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

Improve verbose output by wrapping skip/xfail reasons with margin #10958

Merged
merged 4 commits into from
May 6, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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 AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Benjamin Peterson
Bernard Pratz
Bob Ippolito
Brian Dorsey
Brian Larsen
Brian Maissy
Brian Okken
Brianna Laugher
Expand Down
1 change: 1 addition & 0 deletions changelog/10940.improvement.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added :func:`TerminalReporter.wrap_write() <pytest.TerminalReporter.wrap_write>`, which improves verbose output by performing text wrapping on skip and xfail reasons while leaving a clear margin for progress output.
brl0 marked this conversation as resolved.
Show resolved Hide resolved
nicoddemus marked this conversation as resolved.
Show resolved Hide resolved
25 changes: 24 additions & 1 deletion src/_pytest/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import inspect
import platform
import sys
import textwrap
import warnings
from collections import Counter
from functools import partial
Expand Down Expand Up @@ -426,6 +427,28 @@
self._tw.line()
self.currentfspath = None

def wrap_write(
Copy link
Member

Choose a reason for hiding this comment

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

Not sure "wrap" is appropriated, given "wrap" in this contents is to preserve the text, but wrapping it around at certain width (for example 70). I think what we are doing here is actually truncating, so perhaps truncate_write or something would be more appropriate?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Happy to rename to whatever, but in this case, the new method is only wrapping text. The truncated output you saw was unmodified behavior due to the test testing multiple outputs.
I'm not particularly satisfied with the name because I'm not sure that it feels sufficiently conventional. Perhaps write_wrapped_text or something similar would be better?

Copy link
Member

Choose a reason for hiding this comment

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

Indeed, I misunderstood the output hence my misguided suggestion. wrap_write is fine, thanks! 👍

self,
content: str,
*,
flush: bool = False,
margin: int = 8,
line_sep: str = "\n",
**markup: bool,
) -> None:
"""Wrap message with margin for progress info."""
width_of_current_line = self._tw.width_of_current_line
wrapped = line_sep.join(

Check warning on line 441 in src/_pytest/terminal.py

View check run for this annotation

Codecov / codecov/patch

src/_pytest/terminal.py#L440-L441

Added lines #L440 - L441 were not covered by tests
textwrap.wrap(
" " * width_of_current_line + content,
width=self._screen_width - margin,
drop_whitespace=True,
replace_whitespace=False,
),
)
wrapped = wrapped[width_of_current_line:]
self._tw.write(wrapped, flush=flush, **markup)

Check warning on line 450 in src/_pytest/terminal.py

View check run for this annotation

Codecov / codecov/patch

src/_pytest/terminal.py#L449-L450

Added lines #L449 - L450 were not covered by tests

def write(self, content: str, *, flush: bool = False, **markup: bool) -> None:
self._tw.write(content, flush=flush, **markup)

Expand Down Expand Up @@ -572,7 +595,7 @@
formatted_reason = f" ({reason})"

if reason and formatted_reason is not None:
self._tw.write(formatted_reason)
self.wrap_write(formatted_reason)

Check warning on line 598 in src/_pytest/terminal.py

View check run for this annotation

Codecov / codecov/patch

src/_pytest/terminal.py#L598

Added line #L598 was not covered by tests
if self._show_progress_info:
self._write_progress_information_filling_space()
else:
Expand Down
14 changes: 8 additions & 6 deletions testing/test_terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,13 +387,13 @@ def test_10():
pytest.xfail("It's 🕙 o'clock")

@pytest.mark.skip(
reason="cannot do foobar because baz is missing due to I don't know what"
reason="1 cannot do foobar because baz is missing due to I don't know what"
)
def test_long_skip():
pass

@pytest.mark.xfail(
reason="cannot do foobar because baz is missing due to I don't know what"
reason="2 cannot do foobar because baz is missing due to I don't know what"
)
def test_long_xfail():
print(1 / 0)
Expand All @@ -417,8 +417,8 @@ def test_long_xfail():
result.stdout.fnmatch_lines(
common_output
+ [
"test_verbose_skip_reason.py::test_long_skip SKIPPED (cannot *...) *",
"test_verbose_skip_reason.py::test_long_xfail XFAIL (cannot *...) *",
"test_verbose_skip_reason.py::test_long_skip SKIPPED (1 cannot *...) *",
"test_verbose_skip_reason.py::test_long_xfail XFAIL (2 cannot *...) *",
]
)

Expand All @@ -428,12 +428,14 @@ def test_long_xfail():
+ [
(
"test_verbose_skip_reason.py::test_long_skip SKIPPED"
" (cannot do foobar because baz is missing due to I don't know what) *"
" (1 cannot do foobar"
),
"because baz is missing due to I don't know what) *",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@nicoddemus,

Thanks for taking a look.

It does not look like it was doing what was intended, or am I missing something?

The test test_verbose_skip_reason runs pytest twice, once with -v, and once with -vv. The output you provided was from the first run, which is less verbose, hence the truncation. I modified the expected output for the second run to expect the output to continue onto a new line in the middle of the output. This comment annotates that modification to help show more clearly that the modified test does check for the changed behavior.
Hopefully that makes sense, but let me know if it needs further clarification or changes to make it more clear. For example, we could break the runs into two separate tests, and move the common inputs and outputs into a fixture rather than the test body.

Copy link
Member

Choose a reason for hiding this comment

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

Ahh right, sorry I missed that, thanks for the explanation. 👍

(
"test_verbose_skip_reason.py::test_long_xfail XFAIL"
" (cannot do foobar because baz is missing due to I don't know what) *"
" (2 cannot do foobar"
),
"because baz is missing due to I don't know what) *",
]
)

Expand Down