Skip to content

Add 'assert_truncate_level' to show the full diff without -vv #10588

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/3962.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
``assert_truncate_level`` option added to be able to get the full diff without using ``-vv``.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By using confval as I mentioned before, we can create a link to it automatically:

Suggested change
``assert_truncate_level`` option added to be able to get the full diff without using ``-vv``.
:confval:`assert_truncate_level` option added to be able to get the full diff without using ``-vv``.

4 changes: 4 additions & 0 deletions doc/en/how-to/output.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great, but in addition to this, we should also include this option in the Configuration Options section in reference.rst, making use of the confval directive, for example:

.. confval:: addopts
Add the specified ``OPTS`` to the set of command line arguments as if they
had been specified by the user. Example: if you have this ini file content:

(Make sure to include the new option in alphabetical order)

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
Expand Down
7 changes: 5 additions & 2 deletions src/_pytest/assertion/truncate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
11 changes: 11 additions & 0 deletions src/_pytest/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 13 additions & 1 deletion testing/test_assertion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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*"])

Expand Down Expand Up @@ -1689,7 +1699,9 @@ def test_raising_repr():
"""
)
result = pytester.runpytest()
result.stdout.fnmatch_lines(["E AssertionError: <exception str() failed>"])
result.stdout.fnmatch_lines(
["E AssertionError: <unprintable AssertionError object>"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change should be reverted it seems.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, it's strange I had an issue locally.

)


def test_issue_1944(pytester: Pytester) -> None:
Expand Down