Skip to content

Commit 71a7452

Browse files
authored
Merge pull request #4607 from oscarbenjamin/long_output
Show full repr with assert a==b and -vv
2 parents 2359663 + 85055a9 commit 71a7452

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ Oliver Bestwalter
176176
Omar Kohl
177177
Omer Hadari
178178
Ondřej Súkup
179+
Oscar Benjamin
179180
Patrick Hayes
180181
Paweł Adamczak
181182
Pedro Algarvio

changelog/2256.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Show full repr with ``assert a==b`` and ``-vv``.

src/_pytest/assertion/util.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ def isiterable(obj):
151151
elif type(left) == type(right) and (isdatacls(left) or isattrs(left)):
152152
type_fn = (isdatacls, isattrs)
153153
explanation = _compare_eq_cls(left, right, verbose, type_fn)
154+
elif verbose:
155+
explanation = _compare_eq_verbose(left, right)
154156
if isiterable(left) and isiterable(right):
155157
expl = _compare_eq_iterable(left, right, verbose)
156158
if explanation is not None:
@@ -236,6 +238,18 @@ def escape_for_readable_diff(binary_text):
236238
return explanation
237239

238240

241+
def _compare_eq_verbose(left, right):
242+
keepends = True
243+
left_lines = repr(left).splitlines(keepends)
244+
right_lines = repr(right).splitlines(keepends)
245+
246+
explanation = []
247+
explanation += [u"-" + line for line in left_lines]
248+
explanation += [u"+" + line for line in right_lines]
249+
250+
return explanation
251+
252+
239253
def _compare_eq_iterable(left, right, verbose=False):
240254
if not verbose:
241255
return [u"Use -v to get the full diff"]

testing/test_assertion.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,30 @@ def test_list_tuples(self):
488488
expl = callequal([(1, 2)], [])
489489
assert len(expl) > 1
490490

491+
def test_repr_verbose(self):
492+
class Nums:
493+
def __init__(self, nums):
494+
self.nums = nums
495+
496+
def __repr__(self):
497+
return str(self.nums)
498+
499+
list_x = list(range(5000))
500+
list_y = list(range(5000))
501+
list_y[len(list_y) // 2] = 3
502+
nums_x = Nums(list_x)
503+
nums_y = Nums(list_y)
504+
505+
assert callequal(nums_x, nums_y) is None
506+
507+
expl = callequal(nums_x, nums_y, verbose=1)
508+
assert "-" + repr(nums_x) in expl
509+
assert "+" + repr(nums_y) in expl
510+
511+
expl = callequal(nums_x, nums_y, verbose=2)
512+
assert "-" + repr(nums_x) in expl
513+
assert "+" + repr(nums_y) in expl
514+
491515
def test_list_bad_repr(self):
492516
class A(object):
493517
def __repr__(self):

0 commit comments

Comments
 (0)