Skip to content

Commit

Permalink
Improve verbose output by wrapping skip/xfail reasons with margin (#1…
Browse files Browse the repository at this point in the history
…0958)

Co-authored-by: Bruno Oliveira <nicoddemus@gmail.com>
  • Loading branch information
brl0 and nicoddemus committed May 6, 2023
1 parent 07eeeb8 commit 7d548c3
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
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
3 changes: 3 additions & 0 deletions changelog/10940.improvement.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Improved verbose output (``-vv``) of ``skip`` and ``xfail`` reasons by performing text wrapping while leaving a clear margin for progress output.

Added :func:`TerminalReporter.wrap_write() <pytest.TerminalReporter.wrap_write>` as a helper for that.
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 @@ def ensure_newline(self) -> None:
self._tw.line()
self.currentfspath = None

def wrap_write(
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(
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)

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 @@ def pytest_runtest_logreport(self, report: TestReport) -> None:
formatted_reason = f" ({reason})"

if reason and formatted_reason is not None:
self._tw.write(formatted_reason)
self.wrap_write(formatted_reason)
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) *",
(
"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

0 comments on commit 7d548c3

Please sign in to comment.