Skip to content

Commit 7d548c3

Browse files
brl0nicoddemus
andauthored
Improve verbose output by wrapping skip/xfail reasons with margin (#10958)
Co-authored-by: Bruno Oliveira <[email protected]>
1 parent 07eeeb8 commit 7d548c3

File tree

4 files changed

+36
-7
lines changed

4 files changed

+36
-7
lines changed

AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ Benjamin Peterson
5656
Bernard Pratz
5757
Bob Ippolito
5858
Brian Dorsey
59+
Brian Larsen
5960
Brian Maissy
6061
Brian Okken
6162
Brianna Laugher

changelog/10940.improvement.rst

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Improved verbose output (``-vv``) of ``skip`` and ``xfail`` reasons by performing text wrapping while leaving a clear margin for progress output.
2+
3+
Added :func:`TerminalReporter.wrap_write() <pytest.TerminalReporter.wrap_write>` as a helper for that.

src/_pytest/terminal.py

+24-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import inspect
99
import platform
1010
import sys
11+
import textwrap
1112
import warnings
1213
from collections import Counter
1314
from functools import partial
@@ -426,6 +427,28 @@ def ensure_newline(self) -> None:
426427
self._tw.line()
427428
self.currentfspath = None
428429

430+
def wrap_write(
431+
self,
432+
content: str,
433+
*,
434+
flush: bool = False,
435+
margin: int = 8,
436+
line_sep: str = "\n",
437+
**markup: bool,
438+
) -> None:
439+
"""Wrap message with margin for progress info."""
440+
width_of_current_line = self._tw.width_of_current_line
441+
wrapped = line_sep.join(
442+
textwrap.wrap(
443+
" " * width_of_current_line + content,
444+
width=self._screen_width - margin,
445+
drop_whitespace=True,
446+
replace_whitespace=False,
447+
),
448+
)
449+
wrapped = wrapped[width_of_current_line:]
450+
self._tw.write(wrapped, flush=flush, **markup)
451+
429452
def write(self, content: str, *, flush: bool = False, **markup: bool) -> None:
430453
self._tw.write(content, flush=flush, **markup)
431454

@@ -572,7 +595,7 @@ def pytest_runtest_logreport(self, report: TestReport) -> None:
572595
formatted_reason = f" ({reason})"
573596

574597
if reason and formatted_reason is not None:
575-
self._tw.write(formatted_reason)
598+
self.wrap_write(formatted_reason)
576599
if self._show_progress_info:
577600
self._write_progress_information_filling_space()
578601
else:

testing/test_terminal.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -387,13 +387,13 @@ def test_10():
387387
pytest.xfail("It's 🕙 o'clock")
388388
389389
@pytest.mark.skip(
390-
reason="cannot do foobar because baz is missing due to I don't know what"
390+
reason="1 cannot do foobar because baz is missing due to I don't know what"
391391
)
392392
def test_long_skip():
393393
pass
394394
395395
@pytest.mark.xfail(
396-
reason="cannot do foobar because baz is missing due to I don't know what"
396+
reason="2 cannot do foobar because baz is missing due to I don't know what"
397397
)
398398
def test_long_xfail():
399399
print(1 / 0)
@@ -417,8 +417,8 @@ def test_long_xfail():
417417
result.stdout.fnmatch_lines(
418418
common_output
419419
+ [
420-
"test_verbose_skip_reason.py::test_long_skip SKIPPED (cannot *...) *",
421-
"test_verbose_skip_reason.py::test_long_xfail XFAIL (cannot *...) *",
420+
"test_verbose_skip_reason.py::test_long_skip SKIPPED (1 cannot *...) *",
421+
"test_verbose_skip_reason.py::test_long_xfail XFAIL (2 cannot *...) *",
422422
]
423423
)
424424

@@ -428,12 +428,14 @@ def test_long_xfail():
428428
+ [
429429
(
430430
"test_verbose_skip_reason.py::test_long_skip SKIPPED"
431-
" (cannot do foobar because baz is missing due to I don't know what) *"
431+
" (1 cannot do foobar"
432432
),
433+
"because baz is missing due to I don't know what) *",
433434
(
434435
"test_verbose_skip_reason.py::test_long_xfail XFAIL"
435-
" (cannot do foobar because baz is missing due to I don't know what) *"
436+
" (2 cannot do foobar"
436437
),
438+
"because baz is missing due to I don't know what) *",
437439
]
438440
)
439441

0 commit comments

Comments
 (0)