From 3e5c9038ec33915aa67b6a6003ca11c9f8484b18 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Fri, 8 Jan 2016 10:58:26 +0100 Subject: [PATCH] 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. --- CHANGELOG.rst | 5 +++++ _pytest/assertion/__init__.py | 13 +++++++++++-- testing/test_assertion.py | 9 ++++++++- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 30909034f..1fe341e4c 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -26,6 +26,10 @@ including removal of the obsolete ``_pytest.assertion.oldinterpret`` module. Thanks `@nicoddemus`_ for the PR (`#1226`_). +* Comparisons now always show up in full when ``CI`` or ``BUILD_NUMBER`` is + found in the environment, even when -vv isn't used. + Thanks `@The-Compiler`_ for the PR. + **Bug Fixes** @@ -44,6 +48,7 @@ .. _@jab: https://github.com/jab .. _@codewarrior0: https://github.com/codewarrior0 .. _@jaraco: https://github.com/jaraco +.. _@The-Compiler: https://github.com/The-Compiler 2.8.6.dev1 diff --git a/_pytest/assertion/__init__.py b/_pytest/assertion/__init__.py index 54742347c..8e820dae2 100644 --- a/_pytest/assertion/__init__.py +++ b/_pytest/assertion/__init__.py @@ -2,6 +2,7 @@ support for presenting detailed information in failing assertions. """ import py +import os import sys from _pytest.monkeypatch import monkeypatch from _pytest.assertion import util @@ -86,6 +87,12 @@ def pytest_collection(session): hook.set_session(session) +def _running_on_ci(): + """Check if we're currently running on a CI system.""" + env_vars = ['CI', 'BUILD_NUMBER'] + return any(var in os.environ for var in env_vars) + + def pytest_runtest_setup(item): """Setup the pytest_assertrepr_compare hook @@ -99,7 +106,8 @@ def pytest_runtest_setup(item): This uses the first result from the hook and then ensures the following: - * Overly verbose explanations are dropped unles -vv was used. + * Overly verbose explanations are dropped unless -vv was used or + running on a CI. * Embedded newlines are escaped to help util.format_explanation() later. * If the rewrite mode is used embedded %-characters are replaced @@ -113,7 +121,8 @@ def pytest_runtest_setup(item): for new_expl in hook_result: if new_expl: if (sum(len(p) for p in new_expl[1:]) > 80*8 - and item.config.option.verbose < 2): + and item.config.option.verbose < 2 + and not _running_on_ci()): show_max = 10 truncated_lines = len(new_expl) - show_max new_expl[show_max:] = [py.builtin._totext( diff --git a/testing/test_assertion.py b/testing/test_assertion.py index cf715470a..34b900f64 100644 --- a/testing/test_assertion.py +++ b/testing/test_assertion.py @@ -405,7 +405,7 @@ def test_sequence_comparison_uses_repr(testdir): ]) -def test_assert_compare_truncate_longmessage(testdir): +def test_assert_compare_truncate_longmessage(monkeypatch, testdir): testdir.makepyfile(r""" def test_long(): a = list(range(200)) @@ -414,6 +414,7 @@ def test_assert_compare_truncate_longmessage(testdir): b = '\n'.join(map(str, b)) assert a == b """) + monkeypatch.delenv('CI', raising=False) result = testdir.runpytest() # without -vv, truncate the message showing a few diff lines only @@ -431,6 +432,12 @@ def test_assert_compare_truncate_longmessage(testdir): "*- 197", ]) + monkeypatch.setenv('CI', '1') + result = testdir.runpytest() + result.stdout.fnmatch_lines([ + "*- 197", + ]) + def test_assertrepr_loaded_per_dir(testdir): testdir.makepyfile(test_base=['def test_base(): assert 1 == 2'])