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.
This commit is contained in:
Matthew Duck 2016-09-19 12:17:26 +01:00
parent dc16fe2bb9
commit dd64d823b9
2 changed files with 12 additions and 4 deletions

View File

@ -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:')]

View File

@ -351,8 +351,16 @@ class TestAssert_reprcompare:
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}"