Improve/revisit CallInfo.__repr__

This commit is contained in:
Daniel Hahler 2019-10-19 11:05:12 +02:00
parent 94c4dd6ad7
commit 15f9568694
2 changed files with 14 additions and 12 deletions

View File

@ -236,16 +236,9 @@ class CallInfo:
return cls(start=start, stop=stop, when=when, result=result, excinfo=excinfo)
def __repr__(self):
if self.excinfo is not None:
status = "exception"
value = self.excinfo.value
else:
# TODO: investigate unification
value = repr(self._result)
status = "result"
return "<CallInfo when={when!r} {status}: {value}>".format(
when=self.when, value=value, status=status
)
if self.excinfo is None:
return "<CallInfo when={!r} result: {!r}>".format(self.when, self._result)
return "<CallInfo when={!r} excinfo={!r}>".format(self.when, self.excinfo)
def pytest_runtest_makereport(item, call):

View File

@ -483,13 +483,22 @@ def test_callinfo():
assert ci.result == 0
assert "result" in repr(ci)
assert repr(ci) == "<CallInfo when='123' result: 0>"
assert str(ci) == "<CallInfo when='123' result: 0>"
ci = runner.CallInfo.from_call(lambda: 0 / 0, "123")
assert ci.when == "123"
assert not hasattr(ci, "result")
assert repr(ci) == "<CallInfo when='123' exception: division by zero>"
assert repr(ci) == "<CallInfo when='123' excinfo={!r}>".format(ci.excinfo)
assert str(ci) == repr(ci)
assert ci.excinfo
assert "exc" in repr(ci)
# Newlines are escaped.
def raise_assertion():
assert 0, "assert_msg"
ci = runner.CallInfo.from_call(raise_assertion, "call")
assert repr(ci) == "<CallInfo when='call' excinfo={!r}>".format(ci.excinfo)
assert "\n" not in repr(ci)
# design question: do we want general hooks in python files?