From dd64d823b90f87fca2bb77a61da0912d91f8d7ea Mon Sep 17 00:00:00 2001 From: Matthew Duck Date: Mon, 19 Sep 2016 12:17:26 +0100 Subject: [PATCH 1/3] Don't display dict common items if verbosity=1 Part one of https://github.com/pytest-dev/pytest/issues/1512. If verbosity=1, assertion explanations are truncated at 10 lines. In this situation, it's more important to tell the user which dictionary items are different than which are the same. --- _pytest/assertion/util.py | 4 ++-- testing/test_assertion.py | 12 ++++++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/_pytest/assertion/util.py b/_pytest/assertion/util.py index 29ae51186d6..709fe3b4d30 100644 --- a/_pytest/assertion/util.py +++ b/_pytest/assertion/util.py @@ -256,8 +256,8 @@ def _compare_eq_dict(left, right, verbose=False): explanation = [] common = set(left).intersection(set(right)) same = dict((k, left[k]) for k in common if left[k] == right[k]) - if same and not verbose: - explanation += [u('Omitting %s identical items, use -v to show') % + if same and verbose < 2: + explanation += [u('Omitting %s identical items, use -vv to show') % len(same)] elif same: explanation += [u('Common items:')] diff --git a/testing/test_assertion.py b/testing/test_assertion.py index d49815181c0..93e8847fa41 100644 --- a/testing/test_assertion.py +++ b/testing/test_assertion.py @@ -351,8 +351,16 @@ def test_dict_omitting(self): for line in lines[1:]: assert 'b' not in line - def test_dict_omitting_verbose(self): - lines = callequal({'a': 0, 'b': 1}, {'a': 1, 'b': 1}, verbose=True) + def test_dict_omitting_with_verbosity_1(self): + """ Ensure differing items are visible for verbosity=1 (#1512) """ + lines = callequal({'a': 0, 'b': 1}, {'a': 1, 'b': 1}, verbose=1) + assert lines[1].startswith('Omitting 1 identical item') + assert lines[2].startswith('Differing items') + assert lines[3] == "{'a': 0} != {'a': 1}" + assert 'Common items' not in lines + + def test_dict_omitting_with_verbosity_2(self): + lines = callequal({'a': 0, 'b': 1}, {'a': 1, 'b': 1}, verbose=2) assert lines[1].startswith('Common items:') assert 'Omitting' not in lines[1] assert lines[2] == "{'b': 1}" From 999e7c65417f1e97fc89bf66e0da4c5cd84442ec Mon Sep 17 00:00:00 2001 From: Matthew Duck Date: Mon, 19 Sep 2016 14:19:34 +0100 Subject: [PATCH 2/3] Tidy formatting of assertion truncation Part two of https://github.com/pytest-dev/pytest/issues/1512. Update the format of the truncation message to help make it clear that pytest truncates the entire assertion output when verbosity < 2. --- _pytest/assertion/__init__.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/_pytest/assertion/__init__.py b/_pytest/assertion/__init__.py index fd1ebe2c123..e7f0e58ed6a 100644 --- a/_pytest/assertion/__init__.py +++ b/_pytest/assertion/__init__.py @@ -131,14 +131,21 @@ def callbinrepr(op, left, right): config=item.config, op=op, left=left, right=right) for new_expl in hook_result: if new_expl: + + # Truncate lines if required if (sum(len(p) for p in new_expl[1:]) > 80*8 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( - 'Detailed information truncated (%d more lines)' - ', use "-vv" to show' % truncated_lines)] + truncated_count = len(new_expl) - show_max + new_expl[show_max - 1] += " ..." + new_expl[show_max:] = [ + py.builtin._totext(""), + py.builtin._totext('...Full output truncated (%d more lines)' + ', use "-vv" to show' % truncated_count + ), + ] + new_expl = [line.replace("\n", "\\n") for line in new_expl] res = py.builtin._totext("\n~").join(new_expl) if item.config.getvalue("assertmode") == "rewrite": From 4df74a5cfb20c0898f046315dd5b1d0053922062 Mon Sep 17 00:00:00 2001 From: Matthew Duck Date: Mon, 19 Sep 2016 14:35:00 +0100 Subject: [PATCH 3/3] Add AUTHORS and CHANGELOG for #1512 --- AUTHORS | 1 + CHANGELOG.rst | 10 +++++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/AUTHORS b/AUTHORS index c74aa4af685..a53455aff7d 100644 --- a/AUTHORS +++ b/AUTHORS @@ -89,6 +89,7 @@ Martijn Faassen Martin K. Scherer Martin Prusse Matt Bachmann +Matt Duck Matt Williams Matthias Hafner mbyt diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 7d5bee5a85f..1c75e4335d7 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -4,15 +4,19 @@ * Testcase reports with a url attribute will now properly write this to junitxml. Thanks `@fushi`_ for the PR -* - -* +* Remove common items from dict comparision output when verbosity=1. Also update + the truncation message to make it clearer that pytest truncates all + assertion messages if verbosity < 2 (`#1512`_). + Thanks `@mattduck`_ for the PR * * .. _@fushi: https://github.com/fushi +.. _@mattduck: https://github.com/mattduck + +.. _#1512: https://github.com/pytest-dev/pytest/issues/1512 3.0.2