summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Hahler <git@thequod.de>2019-10-09 05:16:27 +0200
committerDaniel Hahler <git@thequod.de>2019-10-09 19:27:46 +0200
commit2a2fe7d3db12b03b9a62392bccaceacd256c063e (patch)
treec1387d0c7d1f42473aa457bda75b29bcfd4a9e92
parent5c92a0f695c4f302612c50249f0a7bff1047f6b2 (diff)
downloadpytest-2a2fe7d3db12b03b9a62392bccaceacd256c063e.tar.gz
Improve ExceptionInfo.__repr__
-rw-r--r--doc/5934.feature.rst1
-rw-r--r--src/_pytest/_code/code.py4
-rw-r--r--testing/code/test_excinfo.py15
3 files changed, 17 insertions, 3 deletions
diff --git a/doc/5934.feature.rst b/doc/5934.feature.rst
new file mode 100644
index 000000000..17c0b1737
--- /dev/null
+++ b/doc/5934.feature.rst
@@ -0,0 +1 @@
+``repr`` of ``ExceptionInfo`` objects has been improved to honor the ``__repr__`` method of the underlying exception.
diff --git a/src/_pytest/_code/code.py b/src/_pytest/_code/code.py
index 534bfe2a8..1d26d94ab 100644
--- a/src/_pytest/_code/code.py
+++ b/src/_pytest/_code/code.py
@@ -507,7 +507,9 @@ class ExceptionInfo(Generic[_E]):
def __repr__(self) -> str:
if self._excinfo is None:
return "<ExceptionInfo for raises contextmanager>"
- return "<ExceptionInfo %s tblen=%d>" % (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 e2f06a0a2..3f205b131 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) == "<ExceptionInfo ValueError tblen=4>"
- assert str(excinfo) == "<ExceptionInfo ValueError tblen=4>"
+ assert repr(excinfo) == "<ExceptionInfo ValueError() tblen=4>"
+ assert str(excinfo) == "<ExceptionInfo ValueError() tblen=4>"
+
+ class CustomException(Exception):
+ def __repr__(self):
+ return "custom_repr"
+
+ def raises():
+ raise CustomException()
+
+ excinfo = pytest.raises(CustomException, raises)
+ assert repr(excinfo) == "<ExceptionInfo custom_repr tblen=2>"
+ assert str(excinfo) == "<ExceptionInfo custom_repr tblen=2>"
def test_excinfo_for_later():