diff --git a/changelog/3554.bugfix.rst b/changelog/3554.bugfix.rst new file mode 100644 index 00000000000..b4c43cb8f8e --- /dev/null +++ b/changelog/3554.bugfix.rst @@ -0,0 +1 @@ +Fix ``CallInfo.__repr__`` for when the call is not finished yet. diff --git a/src/_pytest/runner.py b/src/_pytest/runner.py index 4d4b06d7cb3..c2de759b35d 100644 --- a/src/_pytest/runner.py +++ b/src/_pytest/runner.py @@ -224,7 +224,8 @@ def __repr__(self): if self.excinfo: status = "exception: %s" % str(self.excinfo.value) else: - status = "result: %r" % (self.result,) + result = getattr(self, "result", "") + status = "result: %r" % (result,) return "" % (self.when, status) diff --git a/testing/test_runner.py b/testing/test_runner.py index 2d55e9e5ad7..c081920a502 100644 --- a/testing/test_runner.py +++ b/testing/test_runner.py @@ -491,13 +491,26 @@ def test_callinfo(): assert ci.when == "123" assert ci.result == 0 assert "result" in repr(ci) + assert repr(ci) == "" + ci = runner.CallInfo(lambda: 0 / 0, "123") assert ci.when == "123" assert not hasattr(ci, "result") + assert repr(ci) == "" assert ci.excinfo assert "exc" in repr(ci) +def test_callinfo_repr_while_running(): + def repr_while_running(): + f = sys._getframe().f_back + assert "func" in f.f_locals + assert repr(f.f_locals["self"]) == "'>" + + ci = runner.CallInfo(repr_while_running, "when") + assert repr(ci) == "" + + # design question: do we want general hooks in python files? # then something like the following functional tests makes sense