File tree 2 files changed +46
-0
lines changed 2 files changed +46
-0
lines changed Original file line number Diff line number Diff line change @@ -215,6 +215,33 @@ debugging frameworks modules OR if pytest itself drops you into a pdb
215
215
session using ```--pdb` `` or similar.
216
216
217
217
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
+
218
245
Changelog
219
246
=========
220
247
Original file line number Diff line number Diff line change @@ -136,6 +136,25 @@ def pytest_report_header(config):
136
136
]
137
137
138
138
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
+
139
158
@pytest .hookimpl (tryfirst = True )
140
159
def pytest_exception_interact (node ):
141
160
"""Stop the timeout when pytest enters pdb in post-mortem mode."""
You can’t perform that action at this time.
0 commit comments