Skip to content

Commit 99451f9

Browse files
Add 'assert_truncate_level' to show the full diff without -vv
Closes #3962 Co-authored-by: Daniel Hahler <[email protected]>
1 parent 0ded329 commit 99451f9

File tree

5 files changed

+34
-3
lines changed

5 files changed

+34
-3
lines changed

changelog/3962.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
``assert_truncate_level`` option added to be able to get the full diff without using ``-vv``.

doc/en/how-to/output.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,10 @@ situations, for example you are shown even fixtures that start with ``_`` if you
270270
Using higher verbosity levels (``-vvv``, ``-vvvv``, ...) is supported, but has no effect in pytest itself at the moment,
271271
however some plugins might make use of higher verbosity.
272272

273+
By default for verbosity inferior to ``-vv`` really long output are truncated. It's also possible to
274+
control the truncation directly using the ``assert_truncate_level`` option. You can use ``assert_truncate_level=0``
275+
to get the full diff regardless of the verbosity level.
276+
273277
.. _`pytest.detailed_failed_tests_usage`:
274278

275279
Producing a detailed summary report

src/_pytest/assertion/truncate.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,12 @@ def truncate_if_required(
2525

2626

2727
def _should_truncate_item(item: Item) -> bool:
28-
"""Whether or not this test item is eligible for truncation."""
28+
"""Whether this test item is eligible for truncation."""
29+
level = item.config.getini("assert_truncate_level")
2930
verbose = item.config.option.verbose
30-
return verbose < 2 and not util.running_on_ci()
31+
if level == "auto":
32+
return verbose < 2 and not util.running_on_ci()
33+
return bool(int(level) > verbose)
3134

3235

3336
def _truncate_explanation(

src/_pytest/terminal.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,17 @@ def pytest_addoption(parser: Parser) -> None:
256256
default="progress",
257257
)
258258

259+
# Experimental.
260+
parser.addini(
261+
"assert_truncate_level",
262+
help=(
263+
"Truncate explanations of assertion failures? "
264+
'("auto" (when verbosity < 2, and not running on CI), '
265+
"or minimum verbosity level to trigger it (i.e. 0 for no truncation)."
266+
),
267+
default="auto",
268+
)
269+
259270

260271
def pytest_configure(config: Config) -> None:
261272
reporter = TerminalReporter(config, sys.stdout)

testing/test_assertion.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1290,6 +1290,16 @@ def test_many_lines():
12901290
]
12911291
)
12921292

1293+
# test assert_truncate_level ini option.
1294+
result = pytester.runpytest("-o", "assert_truncate_level=1")
1295+
result.stdout.fnmatch_lines(
1296+
["E ...Full output truncated (2 lines hidden), use '-vv' to show"]
1297+
)
1298+
result = pytester.runpytest("-o", "assert_truncate_level=0")
1299+
result.stdout.fnmatch_lines(["* 6*"])
1300+
result = pytester.runpytest("-v", "-o", "assert_truncate_level=0")
1301+
result.stdout.fnmatch_lines(["* 6*"])
1302+
12931303
result = pytester.runpytest("-vv")
12941304
result.stdout.fnmatch_lines(["* 6*"])
12951305

@@ -1689,7 +1699,9 @@ def test_raising_repr():
16891699
"""
16901700
)
16911701
result = pytester.runpytest()
1692-
result.stdout.fnmatch_lines(["E AssertionError: <exception str() failed>"])
1702+
result.stdout.fnmatch_lines(
1703+
["E AssertionError: <unprintable AssertionError object>"]
1704+
)
16931705

16941706

16951707
def test_issue_1944(pytester: Pytester) -> None:

0 commit comments

Comments
 (0)