From d89b5057cadb8716e25b783fee8a1d670e043109 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Fri, 14 Feb 2020 02:17:05 +0100 Subject: [PATCH] assertrepr_compare: provide more info (location) with exceptions (#6728) --- src/_pytest/assertion/util.py | 7 ++++--- testing/test_assertion.py | 12 ++++++++++-- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/_pytest/assertion/util.py b/src/_pytest/assertion/util.py index 51a25490f..7d525aa4c 100644 --- a/src/_pytest/assertion/util.py +++ b/src/_pytest/assertion/util.py @@ -175,9 +175,10 @@ def assertrepr_compare(config, op: str, left: Any, right: Any) -> Optional[List[ raise except Exception: explanation = [ - "(pytest_assertion plugin: representation of details failed. " - "Probably an object has a faulty __repr__.)", - str(_pytest._code.ExceptionInfo.from_current()), + "(pytest_assertion plugin: representation of details failed: {}.".format( + _pytest._code.ExceptionInfo.from_current()._getreprcrash() + ), + " Probably an object has a faulty __repr__.)", ] if not explanation: diff --git a/testing/test_assertion.py b/testing/test_assertion.py index 0c0f12340..cf80fba43 100644 --- a/testing/test_assertion.py +++ b/testing/test_assertion.py @@ -677,8 +677,16 @@ class TestAssert_reprcompare: expl = callequal([], [A()]) assert "ValueError" in "".join(expl) - expl = callequal({}, {"1": A()}) - assert "faulty" in "".join(expl) + expl = callequal({}, {"1": A()}, verbose=2) + assert expl[0].startswith("{} == <[ValueError") + assert "raised in repr" in expl[0] + assert expl[1:] == [ + "(pytest_assertion plugin: representation of details failed:" + " {}:{}: ValueError: 42.".format( + __file__, A.__repr__.__code__.co_firstlineno + 1 + ), + " Probably an object has a faulty __repr__.)", + ] def test_one_repr_empty(self): """