From 089116bdff4a82b624caa50913910e11cfad2d51 Mon Sep 17 00:00:00 2001 From: HolyMagician03-UMich <122073322+HolyMagician03-UMich@users.noreply.github.com> Date: Wed, 10 Apr 2024 07:07:18 -0400 Subject: [PATCH] short test summary: do not truncate text when -vv is given Fix #11777 --------- Co-authored-by: Bruno Oliveira --- AUTHORS | 2 ++ changelog/11777.improvement.rst | 1 + src/_pytest/terminal.py | 6 ++--- testing/test_terminal.py | 46 +++++++++++++++++++++++++++++++-- 4 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 changelog/11777.improvement.rst diff --git a/AUTHORS b/AUTHORS index 53f7a8c2a..cc054bfd9 100644 --- a/AUTHORS +++ b/AUTHORS @@ -137,6 +137,7 @@ Endre Galaczi Eric Hunsberger Eric Liu Eric Siegerman +Eric Yuan Erik Aronesty Erik Hasse Erik M. Bray @@ -432,6 +433,7 @@ Xixi Zhao Xuan Luong Xuecong Liao Yannick PΓ©roux +Yao Xiao Yoav Caspi Yuliang Shao Yusuke Kadowaki diff --git a/changelog/11777.improvement.rst b/changelog/11777.improvement.rst new file mode 100644 index 000000000..fb53c63c1 --- /dev/null +++ b/changelog/11777.improvement.rst @@ -0,0 +1 @@ +Text is no longer truncated in the ``short test summary info`` section when ``-vv`` is given. diff --git a/src/_pytest/terminal.py b/src/_pytest/terminal.py index 973168dc6..724d5c54d 100644 --- a/src/_pytest/terminal.py +++ b/src/_pytest/terminal.py @@ -1408,11 +1408,11 @@ def _get_line_with_reprcrash_message( except AttributeError: pass else: - if not running_on_ci(): + if running_on_ci() or config.option.verbose >= 2: + msg = f" - {msg}" + else: available_width = tw.fullwidth - line_width msg = _format_trimmed(" - {}", msg, available_width) - else: - msg = f" - {msg}" if msg is not None: line += msg diff --git a/testing/test_terminal.py b/testing/test_terminal.py index f49425109..170f1efcf 100644 --- a/testing/test_terminal.py +++ b/testing/test_terminal.py @@ -2377,8 +2377,13 @@ def test_line_with_reprcrash(monkeypatch: MonkeyPatch) -> None: monkeypatch.setattr(_pytest.terminal, "_get_node_id_with_markup", mock_get_pos) + class Namespace: + def __init__(self, **kwargs): + self.__dict__.update(kwargs) + class config: - pass + def __init__(self): + self.option = Namespace(verbose=0) class rep: def _get_verbose_word(self, *args): @@ -2399,7 +2404,7 @@ def test_line_with_reprcrash(monkeypatch: MonkeyPatch) -> None: if msg: rep.longrepr.reprcrash.message = msg # type: ignore actual = _get_line_with_reprcrash_message( - config, # type: ignore[arg-type] + config(), # type: ignore[arg-type] rep(), # type: ignore[arg-type] DummyTerminalWriter(), # type: ignore[arg-type] {}, @@ -2443,6 +2448,43 @@ def test_line_with_reprcrash(monkeypatch: MonkeyPatch) -> None: check("πŸ‰πŸ‰πŸ‰πŸ‰πŸ‰\n2nd line", 80, "FAILED nodeid::πŸ‰::withunicode - πŸ‰πŸ‰πŸ‰πŸ‰πŸ‰") +def test_short_summary_with_verbose( + monkeypatch: MonkeyPatch, pytester: Pytester +) -> None: + """With -vv do not truncate the summary info (#11777).""" + # On CI we also do not truncate the summary info, monkeypatch it to ensure we + # are testing against the -vv flag on CI. + monkeypatch.setattr(_pytest.terminal, "running_on_ci", lambda: False) + + string_length = 200 + pytester.makepyfile( + f""" + def test(): + s1 = "A" * {string_length} + s2 = "B" * {string_length} + assert s1 == s2 + """ + ) + + # No -vv, summary info should be truncated. + result = pytester.runpytest() + result.stdout.fnmatch_lines( + [ + "*short test summary info*", + "* assert 'AAA...", + ], + ) + + # No truncation with -vv. + result = pytester.runpytest("-vv") + result.stdout.fnmatch_lines( + [ + "*short test summary info*", + f"*{'A' * string_length}*{'B' * string_length}'", + ] + ) + + @pytest.mark.parametrize( "seconds, expected", [