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

Co-authored-by: Bruno Oliveira <nicoddemus@gmail.com>
This commit is contained in:
Brian Larsen 2023-05-06 10:15:11 -05:00 committed by GitHub
parent 07eeeb8dfc
commit 7d548c38e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 7 deletions

View File

@ -56,6 +56,7 @@ Benjamin Peterson
Bernard Pratz Bernard Pratz
Bob Ippolito Bob Ippolito
Brian Dorsey Brian Dorsey
Brian Larsen
Brian Maissy Brian Maissy
Brian Okken Brian Okken
Brianna Laugher Brianna Laugher

View File

@ -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.

View File

@ -8,6 +8,7 @@ import datetime
import inspect import inspect
import platform import platform
import sys import sys
import textwrap
import warnings import warnings
from collections import Counter from collections import Counter
from functools import partial from functools import partial
@ -426,6 +427,28 @@ class TerminalReporter:
self._tw.line() self._tw.line()
self.currentfspath = None 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: def write(self, content: str, *, flush: bool = False, **markup: bool) -> None:
self._tw.write(content, flush=flush, **markup) self._tw.write(content, flush=flush, **markup)
@ -572,7 +595,7 @@ class TerminalReporter:
formatted_reason = f" ({reason})" formatted_reason = f" ({reason})"
if reason and formatted_reason is not None: if reason and formatted_reason is not None:
self._tw.write(formatted_reason) self.wrap_write(formatted_reason)
if self._show_progress_info: if self._show_progress_info:
self._write_progress_information_filling_space() self._write_progress_information_filling_space()
else: else:

View File

@ -387,13 +387,13 @@ class TestTerminal:
pytest.xfail("It's 🕙 o'clock") pytest.xfail("It's 🕙 o'clock")
@pytest.mark.skip( @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(): def test_long_skip():
pass pass
@pytest.mark.xfail( @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(): def test_long_xfail():
print(1 / 0) print(1 / 0)
@ -417,8 +417,8 @@ class TestTerminal:
result.stdout.fnmatch_lines( result.stdout.fnmatch_lines(
common_output common_output
+ [ + [
"test_verbose_skip_reason.py::test_long_skip SKIPPED (cannot *...) *", "test_verbose_skip_reason.py::test_long_skip SKIPPED (1 cannot *...) *",
"test_verbose_skip_reason.py::test_long_xfail XFAIL (cannot *...) *", "test_verbose_skip_reason.py::test_long_xfail XFAIL (2 cannot *...) *",
] ]
) )
@ -428,12 +428,14 @@ class TestTerminal:
+ [ + [
( (
"test_verbose_skip_reason.py::test_long_skip SKIPPED" "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" "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) *",
] ]
) )