Skip to content

Commit 4db168c

Browse files
committed
Set timed_out attrib on reports for logging customization
In case the test timed out, set `report.timed_out = True`. That makes it easier to customize the output in case of timeouts. This commit also adds a small section to the README including a small example.
1 parent fb28b8b commit 4db168c

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

README.rst

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,33 @@ debugging frameworks modules OR if pytest itself drops you into a pdb
215215
session using ```--pdb``` or similar.
216216

217217

218+
Logging Customization
219+
=====================
220+
221+
This plugin sets a new ``timed_out`` attribute on report objects via
222+
the ``pytest_runtest_makereport`` hook. This way logging can be
223+
customized in your ``conftest.py`` by implementing
224+
``pytest_report_teststatus``::
225+
226+
@pytest.hookimpl(tryfirst=True)
227+
def pytest_report_teststatus(report, **kwargs):
228+
"""Adapted from
229+
https://github.com/pytest-dev/pytest/blob/38d8deb74d95077ebf189440ca047e14f8197da1/src/_pytest/runner.py#L202
230+
"""
231+
d = "%.2f" % getattr(report, "duration", -1.0)
232+
if report.passed:
233+
return "passed", "P", "PASSED (%s)" % d
234+
if getattr(report, "timed_out", False):
235+
return "failed", "T", "TIMEOUT (%s)" % d
236+
if report.failed:
237+
return "failed", "F", "FAILED (%s)" % d
238+
return None
239+
240+
This will print ``T`` (or ``TIMEOUT (5.00)`` if verbose) in case a test
241+
times out. You might want to restrict this to the *call* phase because
242+
the above code would print three symbols (lines) per test; one for each of
243+
the three test phases *setup*, *call* and *teardown* (use ``report.when``).
244+
218245
Changelog
219246
=========
220247

pytest_timeout.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,25 @@ def pytest_report_header(config):
136136
]
137137

138138

139+
@pytest.hookimpl(hookwrapper=True)
140+
def pytest_runtest_makereport(item, call):
141+
"""Set report.timed_out after it is generated by pytest_runtest_makereport.
142+
143+
This method is a wrapper that sets timed_out if the call has a matching excinfo
144+
value. Note that in that case report.failed and report.timed_out are true (both!).
145+
"""
146+
r = yield
147+
if not hasattr(r, "get_result"):
148+
return
149+
report = r.get_result()
150+
timed_out = False
151+
if hasattr(call.excinfo, "value"):
152+
msg = getattr(call.excinfo.value, "msg", None)
153+
if isinstance(msg, str) and msg.startswith("Timeout >"):
154+
timed_out = True
155+
report.timed_out = timed_out
156+
157+
139158
@pytest.hookimpl(tryfirst=True)
140159
def pytest_exception_interact(node):
141160
"""Stop the timeout when pytest enters pdb in post-mortem mode."""

0 commit comments

Comments
 (0)