diff --git a/src/_pytest/assertion/util.py b/src/_pytest/assertion/util.py index 613a40b97..d20c2363e 100644 --- a/src/_pytest/assertion/util.py +++ b/src/_pytest/assertion/util.py @@ -136,7 +136,10 @@ def isiterable(obj: Any) -> bool: return False except Exception as e: raise ValueError( - f"Unexpected exception {e!r} while testing object {obj!r}. Probably an issue with __iter__" + [ + f"pytest_assertion plugin: unexpected exception {e!r} while testing object {obj!r}", + ", probably from __iter__", + ] ) @@ -203,9 +206,9 @@ def assertrepr_compare( if ( isinstance(e, ValueError) and (len(e.args) == 1) - and ("Unexpected exception" in str(e.args[0])) + and ("__iter__" in str(e.args[0])) ): - explanation = [e.args[0]] + explanation = e.args[0] else: explanation = [ "(pytest_assertion plugin: representation of details failed: {}.".format( diff --git a/testing/test_assertrewrite.py b/testing/test_assertrewrite.py index b4e3e5d53..62c6bb8d4 100644 --- a/testing/test_assertrewrite.py +++ b/testing/test_assertrewrite.py @@ -698,7 +698,7 @@ class TestAssertionRewrite: msg = getmsg(f) assert msg is not None - assert "Unexpected exception" in msg + assert "__iter__" in msg and "__repr__" not in msg def test_formatchar(self) -> None: def f() -> None: