Skip to content

Commit 887c097

Browse files
authored
Merge pull request #1951 from mattduck/feat/1512-dict-compare-output
Feat/1512 dict compare output
2 parents 01db0f1 + 4df74a5 commit 887c097

File tree

5 files changed

+31
-11
lines changed

5 files changed

+31
-11
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ Martijn Faassen
8989
Martin K. Scherer
9090
Martin Prusse
9191
Matt Bachmann
92+
Matt Duck
9293
Matt Williams
9394
Matthias Hafner
9495
mbyt

CHANGELOG.rst

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,19 @@
44
* Testcase reports with a url attribute will now properly write this to junitxml.
55
Thanks `@fushi`_ for the PR
66

7-
*
8-
9-
*
7+
* Remove common items from dict comparision output when verbosity=1. Also update
8+
the truncation message to make it clearer that pytest truncates all
9+
assertion messages if verbosity < 2 (`#1512`_).
10+
Thanks `@mattduck`_ for the PR
1011

1112
*
1213

1314
*
1415

1516
.. _@fushi: https://github.com/fushi
17+
.. _@mattduck: https://github.com/mattduck
18+
19+
.. _#1512: https://github.com/pytest-dev/pytest/issues/1512
1620

1721

1822
3.0.2

_pytest/assertion/__init__.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,21 @@ def callbinrepr(op, left, right):
131131
config=item.config, op=op, left=left, right=right)
132132
for new_expl in hook_result:
133133
if new_expl:
134+
135+
# Truncate lines if required
134136
if (sum(len(p) for p in new_expl[1:]) > 80*8 and
135137
item.config.option.verbose < 2 and
136138
not _running_on_ci()):
137139
show_max = 10
138-
truncated_lines = len(new_expl) - show_max
139-
new_expl[show_max:] = [py.builtin._totext(
140-
'Detailed information truncated (%d more lines)'
141-
', use "-vv" to show' % truncated_lines)]
140+
truncated_count = len(new_expl) - show_max
141+
new_expl[show_max - 1] += " ..."
142+
new_expl[show_max:] = [
143+
py.builtin._totext(""),
144+
py.builtin._totext('...Full output truncated (%d more lines)'
145+
', use "-vv" to show' % truncated_count
146+
),
147+
]
148+
142149
new_expl = [line.replace("\n", "\\n") for line in new_expl]
143150
res = py.builtin._totext("\n~").join(new_expl)
144151
if item.config.getvalue("assertmode") == "rewrite":

_pytest/assertion/util.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,8 @@ def _compare_eq_dict(left, right, verbose=False):
256256
explanation = []
257257
common = set(left).intersection(set(right))
258258
same = dict((k, left[k]) for k in common if left[k] == right[k])
259-
if same and not verbose:
260-
explanation += [u('Omitting %s identical items, use -v to show') %
259+
if same and verbose < 2:
260+
explanation += [u('Omitting %s identical items, use -vv to show') %
261261
len(same)]
262262
elif same:
263263
explanation += [u('Common items:')]

testing/test_assertion.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,16 @@ def test_dict_omitting(self):
351351
for line in lines[1:]:
352352
assert 'b' not in line
353353

354-
def test_dict_omitting_verbose(self):
355-
lines = callequal({'a': 0, 'b': 1}, {'a': 1, 'b': 1}, verbose=True)
354+
def test_dict_omitting_with_verbosity_1(self):
355+
""" Ensure differing items are visible for verbosity=1 (#1512) """
356+
lines = callequal({'a': 0, 'b': 1}, {'a': 1, 'b': 1}, verbose=1)
357+
assert lines[1].startswith('Omitting 1 identical item')
358+
assert lines[2].startswith('Differing items')
359+
assert lines[3] == "{'a': 0} != {'a': 1}"
360+
assert 'Common items' not in lines
361+
362+
def test_dict_omitting_with_verbosity_2(self):
363+
lines = callequal({'a': 0, 'b': 1}, {'a': 1, 'b': 1}, verbose=2)
356364
assert lines[1].startswith('Common items:')
357365
assert 'Omitting' not in lines[1]
358366
assert lines[2] == "{'b': 1}"

0 commit comments

Comments
 (0)