Skip to content

Commit 27dab4e

Browse files
committed
Fix CallInfo.__repr__ for unfinished call
Fixes #3554 Ref: #3560 Ref: #3562
1 parent 243d898 commit 27dab4e

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

changelog/3554.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix ``CallInfo.__repr__`` for when the call is not finished yet.

src/_pytest/runner.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,8 @@ def __repr__(self):
224224
if self.excinfo:
225225
status = "exception: %s" % str(self.excinfo.value)
226226
else:
227-
status = "result: %r" % (self.result,)
227+
result = getattr(self, "result", "<NOTSET>")
228+
status = "result: %r" % (result,)
228229
return "<CallInfo when=%r %s>" % (self.when, status)
229230

230231

testing/test_runner.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,13 +491,26 @@ def test_callinfo():
491491
assert ci.when == "123"
492492
assert ci.result == 0
493493
assert "result" in repr(ci)
494+
assert repr(ci) == "<CallInfo when='123' result: 0>"
495+
494496
ci = runner.CallInfo(lambda: 0 / 0, "123")
495497
assert ci.when == "123"
496498
assert not hasattr(ci, "result")
499+
assert repr(ci) == "<CallInfo when='123' exception: division by zero>"
497500
assert ci.excinfo
498501
assert "exc" in repr(ci)
499502

500503

504+
def test_callinfo_repr_while_running():
505+
def repr_while_running():
506+
f = sys._getframe().f_back
507+
assert "func" in f.f_locals
508+
assert repr(f.f_locals["self"]) == "<CallInfo when='when' result: '<NOTSET>'>"
509+
510+
ci = runner.CallInfo(repr_while_running, "when")
511+
assert repr(ci) == "<CallInfo when='when' result: None>"
512+
513+
501514
# design question: do we want general hooks in python files?
502515
# then something like the following functional tests makes sense
503516

0 commit comments

Comments
 (0)