Skip to content

Commit

Permalink
Merge pull request #1951 from mattduck/feat/1512-dict-compare-output
Browse files Browse the repository at this point in the history
Feat/1512 dict compare output
  • Loading branch information
flub authored Sep 19, 2016
2 parents 01db0f1 + 4df74a5 commit 887c097
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 11 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ Martijn Faassen
Martin K. Scherer
Martin Prusse
Matt Bachmann
Matt Duck
Matt Williams
Matthias Hafner
mbyt
Expand Down
10 changes: 7 additions & 3 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 11 additions & 4 deletions _pytest/assertion/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
4 changes: 2 additions & 2 deletions _pytest/assertion/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:')]
Expand Down
12 changes: 10 additions & 2 deletions testing/test_assertion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down

0 comments on commit 887c097

Please sign in to comment.