Skip to content

Commit d7f3665

Browse files
committed
Also handle logstart and logfinish hooks
1 parent 89a55d8 commit d7f3665

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

_pytest/logging.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -371,18 +371,20 @@ 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-
if not hasattr(item, 'catch_log_handlers'):
374+
if item and not hasattr(item, 'catch_log_handlers'):
375375
item.catch_log_handlers = {}
376-
item.catch_log_handlers[when] = log_handler
377-
item.catch_log_handler = log_handler
376+
if item:
377+
item.catch_log_handlers[when] = log_handler
378+
item.catch_log_handler = log_handler
378379
try:
379380
yield # run test
380381
finally:
381-
del item.catch_log_handler
382+
if item:
383+
del item.catch_log_handler
382384
if when == 'teardown':
383385
del item.catch_log_handlers
384386

385-
if self.print_logs:
387+
if self.print_logs and item:
386388
# Add a captured log section to the report.
387389
log = log_handler.stream.getvalue().strip()
388390
item.add_report_section(when, 'log', log)
@@ -402,9 +404,17 @@ def pytest_runtest_teardown(self, item):
402404
with self._runtest_for(item, 'teardown'):
403405
yield
404406

407+
@pytest.hookimpl(hookwrapper=True)
405408
def pytest_runtest_logstart(self):
406409
if self.log_cli_handler:
407410
self.log_cli_handler.reset()
411+
with self._runtest_for(None, 'start'):
412+
yield
413+
414+
@pytest.hookimpl(hookwrapper=True)
415+
def pytest_runtest_logfinish(self):
416+
with self._runtest_for(None, 'finish'):
417+
yield
408418

409419
@pytest.hookimpl(hookwrapper=True)
410420
def pytest_runtestloop(self, session):
@@ -474,7 +484,7 @@ def emit(self, record):
474484
if self.capture_manager is not None:
475485
self.capture_manager.suspend_global_capture()
476486
try:
477-
if not self._first_record_emitted or self._when == 'teardown':
487+
if not self._first_record_emitted or self._when in ('teardown', 'finish'):
478488
self.stream.write('\n')
479489
self._first_record_emitted = True
480490
if not self._section_name_shown:

testing/logging/test_reporting.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,12 +226,21 @@ def test_log_2():
226226

227227

228228
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."""
229+
"""Check that with live logging enable we are printing the correct headers during
230+
start/setup/call/teardown/finish."""
230231
filename = request.node.name + '.py'
231232
testdir.makepyfile('''
232233
import pytest
233234
import logging
234235
236+
@pytest.hookimpl(hookwrapper=True)
237+
def pytest_runtest_logstart():
238+
logging.warning('>>>>> START >>>>>')
239+
240+
@pytest.hookimpl(hookwrapper=True)
241+
def pytest_runtest_logfinish():
242+
logging.warning('<<<<< END <<<<<<<')
243+
235244
@pytest.fixture
236245
def fix(request):
237246
logging.warning("log message from setup of {}".format(request.node.name))
@@ -252,22 +261,30 @@ def test_log_2(fix):
252261
result = testdir.runpytest()
253262
result.stdout.fnmatch_lines([
254263
'{}::test_log_1 '.format(filename),
264+
'*-- live log start --*',
265+
'*WARNING*log >>>>> START >>>>> *',
255266
'*-- live log setup --*',
256267
'*WARNING*log message from setup of test_log_1*',
257268
'*-- live log call --*',
258269
'*WARNING*log message from test_log_1*',
259270
'PASSED *50%*',
260271
'*-- live log teardown --*',
261272
'*WARNING*log message from teardown of test_log_1*',
273+
'*-- live log finish --*',
274+
'*WARNING*log <<<<< END <<<<<<< *',
262275

263276
'{}::test_log_2 '.format(filename),
277+
'*-- live log start --*',
278+
'*WARNING*log >>>>> START >>>>> *',
264279
'*-- live log setup --*',
265280
'*WARNING*log message from setup of test_log_2*',
266281
'*-- live log call --*',
267282
'*WARNING*log message from test_log_2*',
268283
'PASSED *100%*',
269284
'*-- live log teardown --*',
270285
'*WARNING*log message from teardown of test_log_2*',
286+
'*-- live log finish --*',
287+
'*WARNING*log <<<<< END <<<<<<< *',
271288
'=* 2 passed in *=',
272289
])
273290

0 commit comments

Comments
 (0)