From 6a8080d2da2abe7f3612a636d9c4c9f93faf950c Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Wed, 9 Oct 2019 05:16:27 +0200 Subject: [PATCH] Improve ExceptionInfo.__repr__ --- src/_pytest/_code/code.py | 4 +++- testing/code/test_excinfo.py | 15 +++++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/_pytest/_code/code.py b/src/_pytest/_code/code.py index 534bfe2a831..1d26d94ab58 100644 --- a/src/_pytest/_code/code.py +++ b/src/_pytest/_code/code.py @@ -507,7 +507,9 @@ def traceback(self, value: Traceback) -> None: def __repr__(self) -> str: if self._excinfo is None: return "" - return "" % (self.typename, len(self.traceback)) + return "<{} {} tblen={}>".format( + self.__class__.__name__, saferepr(self._excinfo[1]), len(self.traceback) + ) def exconly(self, tryshort: bool = False) -> str: """ return the exception as a string diff --git a/testing/code/test_excinfo.py b/testing/code/test_excinfo.py index e2f06a0a253..3f205b131c6 100644 --- a/testing/code/test_excinfo.py +++ b/testing/code/test_excinfo.py @@ -316,8 +316,19 @@ def test_excinfo_exconly(): def test_excinfo_repr_str(): excinfo = pytest.raises(ValueError, h) - assert repr(excinfo) == "" - assert str(excinfo) == "" + assert repr(excinfo) == "" + assert str(excinfo) == "" + + class CustomException(Exception): + def __repr__(self): + return "custom_repr" + + def raises(): + raise CustomException() + + excinfo = pytest.raises(CustomException, raises) + assert repr(excinfo) == "" + assert str(excinfo) == "" def test_excinfo_for_later():