Skip to content

Commit 14b79a6

Browse files
authored
Merge pull request #9025 from davidszotten/more_verbose_for_ci
2 parents 9546fb7 + d5c020d commit 14b79a6

File tree

4 files changed

+34
-10
lines changed

4 files changed

+34
-10
lines changed

changelog/9023.feature.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
Full diffs are now always shown for equality assertions of iterables when
3+
`CI` or ``BUILD_NUMBER`` is found in the environment, even when ``-v`` isn't
4+
used.

src/_pytest/assertion/truncate.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
Current default behaviour is to truncate assertion explanations at
44
~8 terminal lines, unless running in "-vv" mode or running on CI.
55
"""
6-
import os
76
from typing import List
87
from typing import Optional
98

9+
from _pytest.assertion import util
1010
from _pytest.nodes import Item
1111

1212

@@ -27,13 +27,7 @@ def truncate_if_required(
2727
def _should_truncate_item(item: Item) -> bool:
2828
"""Whether or not this test item is eligible for truncation."""
2929
verbose = item.config.option.verbose
30-
return verbose < 2 and not _running_on_ci()
31-
32-
33-
def _running_on_ci() -> bool:
34-
"""Check if we're currently running on a CI system."""
35-
env_vars = ["CI", "BUILD_NUMBER"]
36-
return any(var in os.environ for var in env_vars)
30+
return verbose < 2 and not util.running_on_ci()
3731

3832

3933
def _truncate_explanation(

src/_pytest/assertion/util.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Utilities for assertion debugging."""
22
import collections.abc
3+
import os
34
import pprint
45
from typing import AbstractSet
56
from typing import Any
@@ -17,7 +18,6 @@
1718
from _pytest._io.saferepr import saferepr
1819
from _pytest.config import Config
1920

20-
2121
# The _reprcompare attribute on the util module is used by the new assertion
2222
# interpretation code and assertion rewriter to detect this plugin was
2323
# loaded and in turn call the hooks defined here as part of the
@@ -287,7 +287,7 @@ def _surrounding_parens_on_own_lines(lines: List[str]) -> None:
287287
def _compare_eq_iterable(
288288
left: Iterable[Any], right: Iterable[Any], verbose: int = 0
289289
) -> List[str]:
290-
if not verbose:
290+
if not verbose and not running_on_ci():
291291
return ["Use -v to get the full diff"]
292292
# dynamic import to speedup pytest
293293
import difflib
@@ -490,3 +490,9 @@ def _notin_text(term: str, text: str, verbose: int = 0) -> List[str]:
490490
else:
491491
newdiff.append(line)
492492
return newdiff
493+
494+
495+
def running_on_ci() -> bool:
496+
"""Check if we're currently running on a CI system."""
497+
env_vars = ["CI", "BUILD_NUMBER"]
498+
return any(var in os.environ for var in env_vars)

testing/test_assertion.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from _pytest import outcomes
1414
from _pytest.assertion import truncate
1515
from _pytest.assertion import util
16+
from _pytest.monkeypatch import MonkeyPatch
1617
from _pytest.pytester import Pytester
1718

1819

@@ -448,6 +449,25 @@ def test_iterable_full_diff(self, left, right, expected) -> None:
448449
assert verbose_expl is not None
449450
assert "\n".join(verbose_expl).endswith(textwrap.dedent(expected).strip())
450451

452+
def test_iterable_full_diff_ci(
453+
self, monkeypatch: MonkeyPatch, pytester: Pytester
454+
) -> None:
455+
pytester.makepyfile(
456+
r"""
457+
def test_full_diff():
458+
left = [0, 1]
459+
right = [0, 2]
460+
assert left == right
461+
"""
462+
)
463+
monkeypatch.setenv("CI", "true")
464+
result = pytester.runpytest()
465+
result.stdout.fnmatch_lines(["E Full diff:"])
466+
467+
monkeypatch.delenv("CI", raising=False)
468+
result = pytester.runpytest()
469+
result.stdout.fnmatch_lines(["E Use -v to get the full diff"])
470+
451471
def test_list_different_lengths(self) -> None:
452472
expl = callequal([0, 1], [0, 1, 2])
453473
assert expl is not None

0 commit comments

Comments
 (0)