From ff31dbe02a27f51372e4651684d086a3b906c27c Mon Sep 17 00:00:00 2001 From: Matthew Duck Date: Mon, 19 Sep 2016 12:17:26 +0100 Subject: [PATCH] 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 2481cf34ccd..b6598823d84 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 af7e7e0fed5..4fda7efc692 100644 --- a/testing/test_assertion.py +++ b/testing/test_assertion.py @@ -361,8 +361,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}"