Skip to content

Commit c692679

Browse files
committed
Additionally handle logstart and logfinish hooks
1 parent f2fb841 commit c692679

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

_pytest/logging.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,14 +371,22 @@ def _runtest_for(self, item, when):
371371
formatter=self.formatter, level=self.log_level) as log_handler:
372372
if self.log_cli_handler:
373373
self.log_cli_handler.set_when(when)
374+
375+
if item is None:
376+
yield # run the test
377+
return
378+
374379
if not hasattr(item, 'catch_log_handlers'):
375380
item.catch_log_handlers = {}
381+
376382
item.catch_log_handlers[when] = log_handler
377383
item.catch_log_handler = log_handler
384+
378385
try:
379386
yield # run test
380387
finally:
381-
del item.catch_log_handler
388+
if item:
389+
del item.catch_log_handler
382390
if when == 'teardown':
383391
del item.catch_log_handlers
384392

@@ -402,9 +410,17 @@ def pytest_runtest_teardown(self, item):
402410
with self._runtest_for(item, 'teardown'):
403411
yield
404412

413+
@pytest.hookimpl(hookwrapper=True)
405414
def pytest_runtest_logstart(self):
406415
if self.log_cli_handler:
407416
self.log_cli_handler.reset()
417+
with self._runtest_for(None, 'start'):
418+
yield
419+
420+
@pytest.hookimpl(hookwrapper=True)
421+
def pytest_runtest_logfinish(self):
422+
with self._runtest_for(None, 'finish'):
423+
yield
408424

409425
@pytest.hookimpl(hookwrapper=True)
410426
def pytest_runtestloop(self, session):
@@ -474,7 +490,7 @@ def emit(self, record):
474490
if self.capture_manager is not None:
475491
self.capture_manager.suspend_global_capture()
476492
try:
477-
if not self._first_record_emitted or self._when == 'teardown':
493+
if not self._first_record_emitted or self._when in ('teardown', 'finish'):
478494
self.stream.write('\n')
479495
self._first_record_emitted = True
480496
if not self._section_name_shown:

changelog/3189.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Allow the logging plugin, when live logs are enabled, to handle the hooks `pytest_runtest_logstart` and `pytest_runtest_logfinish`.

testing/logging/test_reporting.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ def test_log_cli():
161161
if enabled:
162162
result.stdout.fnmatch_lines([
163163
'test_log_cli_enabled_disabled.py::test_log_cli ',
164+
'*-- live log call --*',
164165
'test_log_cli_enabled_disabled.py* CRITICAL critical message logged by test',
165166
'PASSED*',
166167
])
@@ -226,8 +227,20 @@ def test_log_2():
226227

227228

228229
def test_log_cli_default_level_sections(testdir, request):
229-
"""Check that with live logging enable we are printing the correct headers during setup/call/teardown."""
230+
"""Check that with live logging enable we are printing the correct headers during
231+
start/setup/call/teardown/finish."""
230232
filename = request.node.name + '.py'
233+
testdir.makeconftest('''
234+
import pytest
235+
import logging
236+
237+
def pytest_runtest_logstart():
238+
logging.warning('>>>>> START >>>>>')
239+
240+
def pytest_runtest_logfinish():
241+
logging.warning('<<<<< END <<<<<<<')
242+
''')
243+
231244
testdir.makepyfile('''
232245
import pytest
233246
import logging
@@ -252,22 +265,30 @@ def test_log_2(fix):
252265
result = testdir.runpytest()
253266
result.stdout.fnmatch_lines([
254267
'{}::test_log_1 '.format(filename),
268+
'*-- live log start --*',
269+
'*WARNING* >>>>> START >>>>>*',
255270
'*-- live log setup --*',
256271
'*WARNING*log message from setup of test_log_1*',
257272
'*-- live log call --*',
258273
'*WARNING*log message from test_log_1*',
259274
'PASSED *50%*',
260275
'*-- live log teardown --*',
261276
'*WARNING*log message from teardown of test_log_1*',
277+
'*-- live log finish --*',
278+
'*WARNING* <<<<< END <<<<<<<*',
262279

263280
'{}::test_log_2 '.format(filename),
281+
'*-- live log start --*',
282+
'*WARNING* >>>>> START >>>>>*',
264283
'*-- live log setup --*',
265284
'*WARNING*log message from setup of test_log_2*',
266285
'*-- live log call --*',
267286
'*WARNING*log message from test_log_2*',
268287
'PASSED *100%*',
269288
'*-- live log teardown --*',
270289
'*WARNING*log message from teardown of test_log_2*',
290+
'*-- live log finish --*',
291+
'*WARNING* <<<<< END <<<<<<<*',
271292
'=* 2 passed in *=',
272293
])
273294

0 commit comments

Comments
 (0)