Skip to content

Commit 47d92a0

Browse files
committed
Add tests and improve messages
1 parent 7f1bf44 commit 47d92a0

File tree

2 files changed

+73
-14
lines changed

2 files changed

+73
-14
lines changed

src/_pytest/assertion/util.py

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -292,16 +292,23 @@ def _compare_eq_sequence(left, right, verbose=0):
292292
explanation += [u"At index %s diff: %r != %r" % (i, left[i], right[i])]
293293
break
294294
len_diff = len_left - len_right
295-
if len_diff > 0:
296-
explanation += [
297-
u"Left contains %d more items, first extra item: %s"
298-
% (len_diff, saferepr(left[len_right]))
299-
]
300-
elif len_diff < 0:
301-
explanation += [
302-
u"Right contains %d more items, first extra item: %s"
303-
% (0 - len_diff, saferepr(right[len_left]))
304-
]
295+
296+
if len_diff:
297+
if len_diff > 0:
298+
dir_with_more = "Left"
299+
extra = saferepr(left[len_right])
300+
elif len_diff < 0:
301+
len_diff = 0 - len_diff
302+
dir_with_more = "Right"
303+
extra = saferepr(right[len_left])
304+
305+
if len_diff == 1:
306+
explanation += [u"%s contains one more item: %s" % (dir_with_more, extra)]
307+
else:
308+
explanation += [
309+
u"%s contains %d more items, first extra item: %s"
310+
% (dir_with_more, len_diff, extra)
311+
]
305312
return explanation
306313

307314

@@ -337,14 +344,22 @@ def _compare_eq_dict(left, right, verbose=0):
337344
for k in diff:
338345
explanation += [saferepr({k: left[k]}) + " != " + saferepr({k: right[k]})]
339346
extra_left = set_left - set_right
340-
if extra_left:
341-
explanation.append(u"Left contains %d more items:" % len(extra_left))
347+
len_extra_left = len(extra_left)
348+
if len_extra_left:
349+
explanation.append(
350+
u"Left contains %d more item%s:"
351+
% (len_extra_left, "" if len_extra_left == 1 else "s")
352+
)
342353
explanation.extend(
343354
pprint.pformat({k: left[k] for k in extra_left}).splitlines()
344355
)
345356
extra_right = set_right - set_left
346-
if extra_right:
347-
explanation.append(u"Right contains %d more items:" % len(extra_right))
357+
len_extra_right = len(extra_right)
358+
if len_extra_right:
359+
explanation.append(
360+
u"Right contains %d more item%s:"
361+
% (len_extra_right, "" if len_extra_right == 1 else "s")
362+
)
348363
explanation.extend(
349364
pprint.pformat({k: right[k] for k in extra_right}).splitlines()
350365
)

testing/test_assertion.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,50 @@ def test_dict_omitting_with_verbosity_2(self):
446446
assert "Omitting" not in lines[1]
447447
assert lines[2] == "{'b': 1}"
448448

449+
def test_dict_different_items(self):
450+
lines = callequal({"a": 0}, {"b": 1, "c": 2}, verbose=2)
451+
assert lines == [
452+
"{'a': 0} == {'b': 1, 'c': 2}",
453+
"Left contains 1 more item:",
454+
"{'a': 0}",
455+
"Right contains 2 more items:",
456+
"{'b': 1, 'c': 2}",
457+
"Full diff:",
458+
"- {'a': 0}",
459+
"+ {'b': 1, 'c': 2}",
460+
]
461+
lines = callequal({"b": 1, "c": 2}, {"a": 0}, verbose=2)
462+
assert lines == [
463+
"{'b': 1, 'c': 2} == {'a': 0}",
464+
"Left contains 2 more items:",
465+
"{'b': 1, 'c': 2}",
466+
"Right contains 1 more item:",
467+
"{'a': 0}",
468+
"Full diff:",
469+
"- {'b': 1, 'c': 2}",
470+
"+ {'a': 0}",
471+
]
472+
473+
def test_sequence_different_items(self):
474+
lines = callequal((1, 2), (3, 4, 5), verbose=2)
475+
assert lines == [
476+
"(1, 2) == (3, 4, 5)",
477+
"At index 0 diff: 1 != 3",
478+
"Right contains one more item: 5",
479+
"Full diff:",
480+
"- (1, 2)",
481+
"+ (3, 4, 5)",
482+
]
483+
lines = callequal((1, 2, 3), (4,), verbose=2)
484+
assert lines == [
485+
"(1, 2, 3) == (4,)",
486+
"At index 0 diff: 1 != 4",
487+
"Left contains 2 more items, first extra item: 2",
488+
"Full diff:",
489+
"- (1, 2, 3)",
490+
"+ (4,)",
491+
]
492+
449493
def test_set(self):
450494
expl = callequal({0, 1}, {0, 2})
451495
assert len(expl) > 1

0 commit comments

Comments
 (0)