Skip to content

Commit 3e5c903

Browse files
committed
Always show full comparison output if on CI.
When you don't get enough information with a test running on a CI, it's quite frustrating, for various reasons: - It's more likely to be a flaky test, so you might not be able to reproduce the failure. - Passing -vv is quite bothersome (creating a temporary commit and reverting it) For those reasons, if something goes wrong on CI, it's good to have as much information as possible.
1 parent eebf5c1 commit 3e5c903

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

CHANGELOG.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626
including removal of the obsolete ``_pytest.assertion.oldinterpret`` module.
2727
Thanks `@nicoddemus`_ for the PR (`#1226`_).
2828

29+
* Comparisons now always show up in full when ``CI`` or ``BUILD_NUMBER`` is
30+
found in the environment, even when -vv isn't used.
31+
Thanks `@The-Compiler`_ for the PR.
32+
2933

3034
**Bug Fixes**
3135

@@ -44,6 +48,7 @@
4448
.. _@jab: https://github.com/jab
4549
.. _@codewarrior0: https://github.com/codewarrior0
4650
.. _@jaraco: https://github.com/jaraco
51+
.. _@The-Compiler: https://github.com/The-Compiler
4752

4853

4954
2.8.6.dev1

_pytest/assertion/__init__.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
support for presenting detailed information in failing assertions.
33
"""
44
import py
5+
import os
56
import sys
67
from _pytest.monkeypatch import monkeypatch
78
from _pytest.assertion import util
@@ -86,6 +87,12 @@ def pytest_collection(session):
8687
hook.set_session(session)
8788

8889

90+
def _running_on_ci():
91+
"""Check if we're currently running on a CI system."""
92+
env_vars = ['CI', 'BUILD_NUMBER']
93+
return any(var in os.environ for var in env_vars)
94+
95+
8996
def pytest_runtest_setup(item):
9097
"""Setup the pytest_assertrepr_compare hook
9198
@@ -99,7 +106,8 @@ def callbinrepr(op, left, right):
99106
100107
This uses the first result from the hook and then ensures the
101108
following:
102-
* Overly verbose explanations are dropped unles -vv was used.
109+
* Overly verbose explanations are dropped unless -vv was used or
110+
running on a CI.
103111
* Embedded newlines are escaped to help util.format_explanation()
104112
later.
105113
* If the rewrite mode is used embedded %-characters are replaced
@@ -113,7 +121,8 @@ def callbinrepr(op, left, right):
113121
for new_expl in hook_result:
114122
if new_expl:
115123
if (sum(len(p) for p in new_expl[1:]) > 80*8
116-
and item.config.option.verbose < 2):
124+
and item.config.option.verbose < 2
125+
and not _running_on_ci()):
117126
show_max = 10
118127
truncated_lines = len(new_expl) - show_max
119128
new_expl[show_max:] = [py.builtin._totext(

testing/test_assertion.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ def test_hello():
405405
])
406406

407407

408-
def test_assert_compare_truncate_longmessage(testdir):
408+
def test_assert_compare_truncate_longmessage(monkeypatch, testdir):
409409
testdir.makepyfile(r"""
410410
def test_long():
411411
a = list(range(200))
@@ -414,6 +414,7 @@ def test_long():
414414
b = '\n'.join(map(str, b))
415415
assert a == b
416416
""")
417+
monkeypatch.delenv('CI', raising=False)
417418

418419
result = testdir.runpytest()
419420
# without -vv, truncate the message showing a few diff lines only
@@ -431,6 +432,12 @@ def test_long():
431432
"*- 197",
432433
])
433434

435+
monkeypatch.setenv('CI', '1')
436+
result = testdir.runpytest()
437+
result.stdout.fnmatch_lines([
438+
"*- 197",
439+
])
440+
434441

435442
def test_assertrepr_loaded_per_dir(testdir):
436443
testdir.makepyfile(test_base=['def test_base(): assert 1 == 2'])

0 commit comments

Comments
 (0)