diff --git a/changelog/3962.feature.rst b/changelog/3962.feature.rst new file mode 100644 index 00000000000..6f96f5b0c68 --- /dev/null +++ b/changelog/3962.feature.rst @@ -0,0 +1 @@ +``assert_truncate_level`` option added to be able to get the full diff without using ``-vv``. diff --git a/doc/en/how-to/output.rst b/doc/en/how-to/output.rst index e8e9af0c70b..55ae4da0d8d 100644 --- a/doc/en/how-to/output.rst +++ b/doc/en/how-to/output.rst @@ -270,6 +270,10 @@ situations, for example you are shown even fixtures that start with ``_`` if you Using higher verbosity levels (``-vvv``, ``-vvvv``, ...) is supported, but has no effect in pytest itself at the moment, however some plugins might make use of higher verbosity. +By default for verbosity inferior to ``-vv`` really long output are truncated. It's also possible to +control the truncation directly using the ``assert_truncate_level`` option. You can use ``assert_truncate_level=0`` +to get the full diff regardless of the verbosity level. + .. _`pytest.detailed_failed_tests_usage`: Producing a detailed summary report diff --git a/src/_pytest/assertion/truncate.py b/src/_pytest/assertion/truncate.py index dfd6f65d281..3ed34e8c854 100644 --- a/src/_pytest/assertion/truncate.py +++ b/src/_pytest/assertion/truncate.py @@ -25,9 +25,12 @@ def truncate_if_required( def _should_truncate_item(item: Item) -> bool: - """Whether or not this test item is eligible for truncation.""" + """Whether this test item is eligible for truncation.""" + level = item.config.getini("assert_truncate_level") verbose = item.config.option.verbose - return verbose < 2 and not util.running_on_ci() + if level == "auto": + return verbose < 2 and not util.running_on_ci() + return bool(int(level) > verbose) def _truncate_explanation( diff --git a/src/_pytest/terminal.py b/src/_pytest/terminal.py index b0cdb58ce00..e3591853b4f 100644 --- a/src/_pytest/terminal.py +++ b/src/_pytest/terminal.py @@ -256,6 +256,17 @@ def pytest_addoption(parser: Parser) -> None: default="progress", ) + # Experimental. + parser.addini( + "assert_truncate_level", + help=( + "Truncate explanations of assertion failures? " + '("auto" (when verbosity < 2, and not running on CI), ' + "or minimum verbosity level to trigger it (i.e. 0 for no truncation)." + ), + default="auto", + ) + def pytest_configure(config: Config) -> None: reporter = TerminalReporter(config, sys.stdout) diff --git a/testing/test_assertion.py b/testing/test_assertion.py index 473ae44d98d..ec09749dadd 100644 --- a/testing/test_assertion.py +++ b/testing/test_assertion.py @@ -1290,6 +1290,16 @@ def test_many_lines(): ] ) + # test assert_truncate_level ini option. + result = pytester.runpytest("-o", "assert_truncate_level=1") + result.stdout.fnmatch_lines( + ["E ...Full output truncated (2 lines hidden), use '-vv' to show"] + ) + result = pytester.runpytest("-o", "assert_truncate_level=0") + result.stdout.fnmatch_lines(["* 6*"]) + result = pytester.runpytest("-v", "-o", "assert_truncate_level=0") + result.stdout.fnmatch_lines(["* 6*"]) + result = pytester.runpytest("-vv") result.stdout.fnmatch_lines(["* 6*"]) @@ -1689,7 +1699,9 @@ def test_raising_repr(): """ ) result = pytester.runpytest() - result.stdout.fnmatch_lines(["E AssertionError: "]) + result.stdout.fnmatch_lines( + ["E AssertionError: "] + ) def test_issue_1944(pytester: Pytester) -> None: