Skip to content

Commit 1aa25ec

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

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-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:

changelog/3175.trivial

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: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,20 @@ 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'
232+
testdir.makeconftest('''
233+
import pytest
234+
import logging
235+
236+
def pytest_runtest_logstart():
237+
logging.warning('>>>>> START >>>>>')
238+
239+
def pytest_runtest_logfinish():
240+
logging.warning('<<<<< END <<<<<<<')
241+
''')
242+
231243
testdir.makepyfile('''
232244
import pytest
233245
import logging
@@ -252,22 +264,30 @@ def test_log_2(fix):
252264
result = testdir.runpytest()
253265
result.stdout.fnmatch_lines([
254266
'{}::test_log_1 '.format(filename),
267+
'*-- live log start --*',
268+
'*WARNING* >>>>> START >>>>>*',
255269
'*-- live log setup --*',
256270
'*WARNING*log message from setup of test_log_1*',
257271
'*-- live log call --*',
258272
'*WARNING*log message from test_log_1*',
259273
'PASSED *50%*',
260274
'*-- live log teardown --*',
261275
'*WARNING*log message from teardown of test_log_1*',
276+
'*-- live log finish --*',
277+
'*WARNING* <<<<< END <<<<<<<*',
262278

263279
'{}::test_log_2 '.format(filename),
280+
'*-- live log start --*',
281+
'*WARNING* >>>>> START >>>>>*',
264282
'*-- live log setup --*',
265283
'*WARNING*log message from setup of test_log_2*',
266284
'*-- live log call --*',
267285
'*WARNING*log message from test_log_2*',
268286
'PASSED *100%*',
269287
'*-- live log teardown --*',
270288
'*WARNING*log message from teardown of test_log_2*',
289+
'*-- live log finish --*',
290+
'*WARNING* <<<<< END <<<<<<<*',
271291
'=* 2 passed in *=',
272292
])
273293

0 commit comments

Comments
 (0)