Skip to content

Commit

Permalink
Merge pull request pytest-dev#4607 from oscarbenjamin/long_output
Browse files Browse the repository at this point in the history
Show full repr with assert a==b and -vv
  • Loading branch information
nicoddemus authored Jan 10, 2019
2 parents 2359663 + 85055a9 commit 71a7452
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ Oliver Bestwalter
Omar Kohl
Omer Hadari
Ondřej Súkup
Oscar Benjamin
Patrick Hayes
Paweł Adamczak
Pedro Algarvio
Expand Down
1 change: 1 addition & 0 deletions changelog/2256.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Show full repr with ``assert a==b`` and ``-vv``.
14 changes: 14 additions & 0 deletions src/_pytest/assertion/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ def isiterable(obj):
elif type(left) == type(right) and (isdatacls(left) or isattrs(left)):
type_fn = (isdatacls, isattrs)
explanation = _compare_eq_cls(left, right, verbose, type_fn)
elif verbose:
explanation = _compare_eq_verbose(left, right)
if isiterable(left) and isiterable(right):
expl = _compare_eq_iterable(left, right, verbose)
if explanation is not None:
Expand Down Expand Up @@ -236,6 +238,18 @@ def escape_for_readable_diff(binary_text):
return explanation


def _compare_eq_verbose(left, right):
keepends = True
left_lines = repr(left).splitlines(keepends)
right_lines = repr(right).splitlines(keepends)

explanation = []
explanation += [u"-" + line for line in left_lines]
explanation += [u"+" + line for line in right_lines]

return explanation


def _compare_eq_iterable(left, right, verbose=False):
if not verbose:
return [u"Use -v to get the full diff"]
Expand Down
24 changes: 24 additions & 0 deletions testing/test_assertion.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,30 @@ def test_list_tuples(self):
expl = callequal([(1, 2)], [])
assert len(expl) > 1

def test_repr_verbose(self):
class Nums:
def __init__(self, nums):
self.nums = nums

def __repr__(self):
return str(self.nums)

list_x = list(range(5000))
list_y = list(range(5000))
list_y[len(list_y) // 2] = 3
nums_x = Nums(list_x)
nums_y = Nums(list_y)

assert callequal(nums_x, nums_y) is None

expl = callequal(nums_x, nums_y, verbose=1)
assert "-" + repr(nums_x) in expl
assert "+" + repr(nums_y) in expl

expl = callequal(nums_x, nums_y, verbose=2)
assert "-" + repr(nums_x) in expl
assert "+" + repr(nums_y) in expl

def test_list_bad_repr(self):
class A(object):
def __repr__(self):
Expand Down

0 comments on commit 71a7452

Please sign in to comment.