Skip to content

Commit 9b673bc

Browse files
authored
Improve/revisit CallInfo.__repr__ (#6007)
2 parents 119bf66 + 15f9568 commit 9b673bc

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

src/_pytest/runner.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -236,16 +236,9 @@ def from_call(cls, func, when, reraise=None):
236236
return cls(start=start, stop=stop, when=when, result=result, excinfo=excinfo)
237237

238238
def __repr__(self):
239-
if self.excinfo is not None:
240-
status = "exception"
241-
value = self.excinfo.value
242-
else:
243-
# TODO: investigate unification
244-
value = repr(self._result)
245-
status = "result"
246-
return "<CallInfo when={when!r} {status}: {value}>".format(
247-
when=self.when, value=value, status=status
248-
)
239+
if self.excinfo is None:
240+
return "<CallInfo when={!r} result: {!r}>".format(self.when, self._result)
241+
return "<CallInfo when={!r} excinfo={!r}>".format(self.when, self.excinfo)
249242

250243

251244
def pytest_runtest_makereport(item, call):

testing/test_runner.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -483,13 +483,22 @@ def test_callinfo():
483483
assert ci.result == 0
484484
assert "result" in repr(ci)
485485
assert repr(ci) == "<CallInfo when='123' result: 0>"
486+
assert str(ci) == "<CallInfo when='123' result: 0>"
486487

487488
ci = runner.CallInfo.from_call(lambda: 0 / 0, "123")
488489
assert ci.when == "123"
489490
assert not hasattr(ci, "result")
490-
assert repr(ci) == "<CallInfo when='123' exception: division by zero>"
491+
assert repr(ci) == "<CallInfo when='123' excinfo={!r}>".format(ci.excinfo)
492+
assert str(ci) == repr(ci)
491493
assert ci.excinfo
492-
assert "exc" in repr(ci)
494+
495+
# Newlines are escaped.
496+
def raise_assertion():
497+
assert 0, "assert_msg"
498+
499+
ci = runner.CallInfo.from_call(raise_assertion, "call")
500+
assert repr(ci) == "<CallInfo when='call' excinfo={!r}>".format(ci.excinfo)
501+
assert "\n" not in repr(ci)
493502

494503

495504
# design question: do we want general hooks in python files?

0 commit comments

Comments
 (0)