Skip to content
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

Implement CLI options to set truncation thresholds #12766

Open
wants to merge 27 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
fc06bae
feat: implement customization of truncation limits
zhukoff-pavel Sep 2, 2024
42e4e9c
create changelog
zhukoff-pavel Sep 2, 2024
85fe194
append authors
zhukoff-pavel Sep 2, 2024
24b075b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 2, 2024
550b725
fix changelog
zhukoff-pavel Sep 2, 2024
ea7d90b
add test
zhukoff-pavel Sep 2, 2024
fff0e7b
fix no_fnmatch_line invocations
zhukoff-pavel Sep 2, 2024
3fd50ad
escape newlines
zhukoff-pavel Sep 2, 2024
868c26e
Change order
zhukoff-pavel Sep 2, 2024
5526cba
Merge branch 'main' into impl-12765
zhukoff-pavel Sep 3, 2024
856bb8f
Merge branch 'main' into impl-12765
zhukoff-pavel Sep 8, 2024
e1edf2c
Rewrite to .ini parameters instead of CLI
zhukoff-pavel Sep 16, 2024
b4b5fb1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 16, 2024
23ea5dd
Merge branch 'main' into impl-12765
zhukoff-pavel Sep 16, 2024
d123ac3
Convert CLI flags to confvals in changelog
zhukoff-pavel Sep 16, 2024
98938ce
Clarify truncation usage order in doc/en/how-to/output.rst
zhukoff-pavel Sep 16, 2024
ae3cd2f
Handle parameters being set to zero
zhukoff-pavel Sep 16, 2024
b2287af
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 16, 2024
eee9e70
Fix typo in test case description
zhukoff-pavel Sep 16, 2024
7027624
Fix failing tests
zhukoff-pavel Sep 16, 2024
6fb0d8b
Add version added to doc/en/how-to/output.rst
zhukoff-pavel Sep 18, 2024
d1e555f
Add reference to truncation-params section in changelog/12765.feature…
zhukoff-pavel Sep 18, 2024
dba7fef
Add section in doc/en/how-to/output.rst
zhukoff-pavel Sep 18, 2024
8dddd73
Refactor _should_truncate_item to _get_truncation_parameters
zhukoff-pavel Sep 18, 2024
c3e8d7b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 18, 2024
8b69cda
Add a couple new test cases
zhukoff-pavel Sep 18, 2024
f9fd936
Merge branch 'main' into impl-12765
zhukoff-pavel Sep 18, 2024
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 AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ Paul Müller
Paul Reece
Pauli Virtanen
Pavel Karateev
Pavel Zhukov
Paweł Adamczak
Pedro Algarvio
Petter Strandmark
Expand Down
1 change: 1 addition & 0 deletions changelog/12765.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Thresholds to trigger snippet truncation can now be set with `--truncation-limit-lines` and `--truncation-limit-chars` command line options.
zhukoff-pavel marked this conversation as resolved.
Show resolved Hide resolved
zhukoff-pavel marked this conversation as resolved.
Show resolved Hide resolved
16 changes: 16 additions & 0 deletions doc/en/how-to/output.rst
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,22 @@ captured output:
By default, parametrized variants of skipped tests are grouped together if
they share the same skip reason. You can use ``--no-fold-skipped`` to print each skipped test separately.


Modifying truncation limits
zhukoff-pavel marked this conversation as resolved.
Show resolved Hide resolved
--------------------------------------------------

zhukoff-pavel marked this conversation as resolved.
Show resolved Hide resolved
Default truncation limits are 8 lines or 640 characters, whichever comes first.
To set custom truncation limits you can use following ``pytest.ini`` file options:

.. code-block:: ini

[pytest]
truncation_limit_lines = 10
truncation_limit_chars = 90

That will cause pytest to truncate the assertions to 10 lines or 90 characters.
zhukoff-pavel marked this conversation as resolved.
Show resolved Hide resolved


Creating resultlog format files
--------------------------------------------------

Expand Down
12 changes: 12 additions & 0 deletions src/_pytest/assertion/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ def pytest_addoption(parser: Parser) -> None:
help="Enables the pytest_assertion_pass hook. "
"Make sure to delete any previously generated pyc cache files.",
)

parser.addini(
"truncation_limit_lines",
default=0,
help="Set threshold of LINES after which truncation will take effect",
)
parser.addini(
"truncation_limit_chars",
default=0,
help=("Set threshold of CHARS after which truncation will take effect"),
)

Config._add_verbosity_ini(
parser,
Config.VERBOSITY_ASSERTIONS,
Expand Down
20 changes: 14 additions & 6 deletions src/_pytest/assertion/truncate.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,24 @@


DEFAULT_MAX_LINES = 8
DEFAULT_MAX_CHARS = 8 * 80
DEFAULT_MAX_CHARS = DEFAULT_MAX_LINES * 80
USAGE_MSG = "use '-vv' to show"


def truncate_if_required(
explanation: list[str], item: Item, max_length: int | None = None
) -> list[str]:
def truncate_if_required(explanation: list[str], item: Item) -> list[str]:
"""Truncate this assertion explanation if the given test item is eligible."""
if _should_truncate_item(item):
return _truncate_explanation(explanation)
max_lines = int(
item.config.getini("truncation_limit_lines") or DEFAULT_MAX_LINES
zhukoff-pavel marked this conversation as resolved.
Show resolved Hide resolved
)
max_chars = int(
item.config.getini("truncation_limit_chars") or DEFAULT_MAX_CHARS
)
return _truncate_explanation(
explanation,
max_lines=max_lines,
max_chars=max_chars,
)
return explanation


Expand All @@ -38,7 +46,7 @@ def _truncate_explanation(
) -> list[str]:
"""Truncate given list of strings that makes up the assertion explanation.

Truncates to either 8 lines, or 640 characters - whichever the input reaches
Truncates to either max_lines, or max_chars - whichever the input reaches
first, taking the truncation explanation into account. The remaining lines
will be replaced by a usage message.
"""
Expand Down
55 changes: 55 additions & 0 deletions testing/test_assertion.py
Original file line number Diff line number Diff line change
Expand Up @@ -1435,6 +1435,61 @@ def test_many_lines():
result = pytester.runpytest()
result.stdout.fnmatch_lines(["* 6*"])

@pytest.mark.parametrize(
["truncation_mode", "truncation_limit", "expected_lines_hidden"],
(
("lines", 3, 3),
("lines", 4, 0),
("chars", 8, 6),
("chars", 9, 0),
zhukoff-pavel marked this conversation as resolved.
Show resolved Hide resolved
),
)
def test_truncation_with_ini(
self,
monkeypatch,
pytester: Pytester,
truncation_mode: str,
truncation_limit: int,
expected_lines_hidden: int,
) -> None:
pytester.makepyfile(
"""\
string_a = "123456789\\n23456789\\n3"
string_b = "123456789\\n23456789\\n4"

def test():
assert string_a == string_b
"""
)

# This test produces 6 lines of diff output or 79 characters
# So the effect should be when threshold is < 4 lines (considering 2 additional lines for explanation)
# Or < 9 characters (considering 70 additional characters for explanation)

monkeypatch.delenv("CI", raising=False)

pytester.makeini(
f"""
[pytest]
truncation_limit_{truncation_mode} = {truncation_limit}
"""
)

result = pytester.runpytest()

if expected_lines_hidden != 0:
result.stdout.fnmatch_lines(
[f"*truncated ({expected_lines_hidden} lines hidden)*"]
)
else:
result.stdout.no_fnmatch_line("*truncated*")
result.stdout.fnmatch_lines(
[
"*- 4*",
"*+ 3*",
]
)


def test_python25_compile_issue257(pytester: Pytester) -> None:
pytester.makepyfile(
Expand Down
Loading