Skip to content

Commit bfb8648

Browse files
authored
Merge pull request #13061 from kenny-y-dev/test-summary-fix
Add --force-short-summary option and extend sequence printing with -vv
2 parents ecff0ba + d471aef commit bfb8648

File tree

5 files changed

+63
-1
lines changed

5 files changed

+63
-1
lines changed

changelog/11777.bugfix.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed issue where sequences were still being shortened even with ``-vv`` verbosity.

changelog/12713.feature.rst

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
New `--force-short-summary` option to force condensed summary output regardless of verbosity level.
2+
3+
This lets users still see condensed summary output of failures for quick reference in log files from job outputs, being especially useful if non-condensed output is very verbose.

src/_pytest/assertion/rewrite.py

+3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
from _pytest._io.saferepr import DEFAULT_REPR_MAX_SIZE
3030
from _pytest._io.saferepr import saferepr
31+
from _pytest._io.saferepr import saferepr_unlimited
3132
from _pytest._version import version
3233
from _pytest.assertion import util
3334
from _pytest.config import Config
@@ -433,6 +434,8 @@ def _saferepr(obj: object) -> str:
433434
return obj.__name__
434435

435436
maxsize = _get_maxsize_for_saferepr(util._config)
437+
if not maxsize:
438+
return saferepr_unlimited(obj).replace("\n", "\\n")
436439
return saferepr(obj, maxsize=maxsize).replace("\n", "\\n")
437440

438441

src/_pytest/terminal.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,13 @@ def pytest_addoption(parser: Parser) -> None:
161161
default=True,
162162
help="Do not fold skipped tests in short summary.",
163163
)
164+
group._addoption(
165+
"--force-short-summary",
166+
action="store_true",
167+
dest="force_short_summary",
168+
default=False,
169+
help="Force condensed summary output regardless of verbosity level.",
170+
)
164171
group._addoption(
165172
"-q",
166173
"--quiet",
@@ -1500,7 +1507,9 @@ def _get_line_with_reprcrash_message(
15001507
except AttributeError:
15011508
pass
15021509
else:
1503-
if running_on_ci() or config.option.verbose >= 2:
1510+
if (
1511+
running_on_ci() or config.option.verbose >= 2
1512+
) and not config.option.force_short_summary:
15041513
msg = f" - {msg}"
15051514
else:
15061515
available_width = tw.fullwidth - line_width

testing/test_terminal.py

+46
Original file line numberDiff line numberDiff line change
@@ -2617,6 +2617,52 @@ def test():
26172617
)
26182618

26192619

2620+
def test_full_sequence_print_with_vv(
2621+
monkeypatch: MonkeyPatch, pytester: Pytester
2622+
) -> None:
2623+
"""Do not truncate sequences in summaries with -vv (#11777)."""
2624+
monkeypatch.setattr(_pytest.terminal, "running_on_ci", lambda: False)
2625+
2626+
pytester.makepyfile(
2627+
"""
2628+
def test_len_list():
2629+
l = list(range(10))
2630+
assert len(l) == 9
2631+
2632+
def test_len_dict():
2633+
d = dict(zip(range(10), range(10)))
2634+
assert len(d) == 9
2635+
"""
2636+
)
2637+
2638+
result = pytester.runpytest("-vv")
2639+
assert result.ret == 1
2640+
result.stdout.fnmatch_lines(
2641+
[
2642+
"*short test summary info*",
2643+
f"*{list(range(10))}*",
2644+
f"*{dict(zip(range(10), range(10)))}*",
2645+
]
2646+
)
2647+
2648+
2649+
def test_force_short_summary(monkeypatch: MonkeyPatch, pytester: Pytester) -> None:
2650+
monkeypatch.setattr(_pytest.terminal, "running_on_ci", lambda: False)
2651+
2652+
pytester.makepyfile(
2653+
"""
2654+
def test():
2655+
assert "a\\n" * 10 == ""
2656+
"""
2657+
)
2658+
2659+
result = pytester.runpytest("-vv", "--force-short-summary")
2660+
assert result.ret == 1
2661+
result.stdout.fnmatch_lines(
2662+
["*short test summary info*", "*AssertionError: assert 'a\\na\\na\\na..."]
2663+
)
2664+
2665+
26202666
@pytest.mark.parametrize(
26212667
"seconds, expected",
26222668
[

0 commit comments

Comments
 (0)