diff --git a/AUTHORS b/AUTHORS index fd98f8141..7ce822b43 100644 --- a/AUTHORS +++ b/AUTHORS @@ -176,6 +176,7 @@ Oliver Bestwalter Omar Kohl Omer Hadari Ondřej Súkup +Oscar Benjamin Patrick Hayes Paweł Adamczak Pedro Algarvio diff --git a/changelog/2256.bugfix.rst b/changelog/2256.bugfix.rst new file mode 100644 index 000000000..9dcd281ce --- /dev/null +++ b/changelog/2256.bugfix.rst @@ -0,0 +1 @@ +Show full repr with ``assert a==b`` and ``-vv``. diff --git a/src/_pytest/assertion/util.py b/src/_pytest/assertion/util.py index cb220def3..b35b6abc5 100644 --- a/src/_pytest/assertion/util.py +++ b/src/_pytest/assertion/util.py @@ -151,6 +151,8 @@ def assertrepr_compare(config, op, left, right): 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: @@ -236,6 +238,18 @@ def _diff_text(left, right, verbose=False): 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"] diff --git a/testing/test_assertion.py b/testing/test_assertion.py index fcefdbb11..cbd0d9068 100644 --- a/testing/test_assertion.py +++ b/testing/test_assertion.py @@ -488,6 +488,30 @@ class TestAssert_reprcompare(object): 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):