Skip to content

Commit

Permalink
Merge pull request #13061 from kenny-y-dev/test-summary-fix
Browse files Browse the repository at this point in the history
Add --force-short-summary option and extend sequence printing with -vv
  • Loading branch information
nicoddemus authored Jan 26, 2025
2 parents ecff0ba + d471aef commit bfb8648
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/11777.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed issue where sequences were still being shortened even with ``-vv`` verbosity.
3 changes: 3 additions & 0 deletions changelog/12713.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
New `--force-short-summary` option to force condensed summary output regardless of verbosity level.

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.
3 changes: 3 additions & 0 deletions src/_pytest/assertion/rewrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

from _pytest._io.saferepr import DEFAULT_REPR_MAX_SIZE
from _pytest._io.saferepr import saferepr
from _pytest._io.saferepr import saferepr_unlimited
from _pytest._version import version
from _pytest.assertion import util
from _pytest.config import Config
Expand Down Expand Up @@ -433,6 +434,8 @@ def _saferepr(obj: object) -> str:
return obj.__name__

maxsize = _get_maxsize_for_saferepr(util._config)
if not maxsize:
return saferepr_unlimited(obj).replace("\n", "\\n")
return saferepr(obj, maxsize=maxsize).replace("\n", "\\n")


Expand Down
11 changes: 10 additions & 1 deletion src/_pytest/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,13 @@ def pytest_addoption(parser: Parser) -> None:
default=True,
help="Do not fold skipped tests in short summary.",
)
group._addoption(
"--force-short-summary",
action="store_true",
dest="force_short_summary",
default=False,
help="Force condensed summary output regardless of verbosity level.",
)
group._addoption(
"-q",
"--quiet",
Expand Down Expand Up @@ -1500,7 +1507,9 @@ def _get_line_with_reprcrash_message(
except AttributeError:
pass
else:
if running_on_ci() or config.option.verbose >= 2:
if (
running_on_ci() or config.option.verbose >= 2
) and not config.option.force_short_summary:
msg = f" - {msg}"
else:
available_width = tw.fullwidth - line_width
Expand Down
46 changes: 46 additions & 0 deletions testing/test_terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2617,6 +2617,52 @@ def test():
)


def test_full_sequence_print_with_vv(
monkeypatch: MonkeyPatch, pytester: Pytester
) -> None:
"""Do not truncate sequences in summaries with -vv (#11777)."""
monkeypatch.setattr(_pytest.terminal, "running_on_ci", lambda: False)

pytester.makepyfile(
"""
def test_len_list():
l = list(range(10))
assert len(l) == 9
def test_len_dict():
d = dict(zip(range(10), range(10)))
assert len(d) == 9
"""
)

result = pytester.runpytest("-vv")
assert result.ret == 1
result.stdout.fnmatch_lines(
[
"*short test summary info*",
f"*{list(range(10))}*",
f"*{dict(zip(range(10), range(10)))}*",
]
)


def test_force_short_summary(monkeypatch: MonkeyPatch, pytester: Pytester) -> None:
monkeypatch.setattr(_pytest.terminal, "running_on_ci", lambda: False)

pytester.makepyfile(
"""
def test():
assert "a\\n" * 10 == ""
"""
)

result = pytester.runpytest("-vv", "--force-short-summary")
assert result.ret == 1
result.stdout.fnmatch_lines(
["*short test summary info*", "*AssertionError: assert 'a\\na\\na\\na..."]
)


@pytest.mark.parametrize(
"seconds, expected",
[
Expand Down

0 comments on commit bfb8648

Please sign in to comment.