Additionally handle logstart and logfinish hooks

This commit is contained in:
Pedro Algarvio 2018-01-31 13:48:55 +00:00
parent f2fb841b29
commit 67558e0e22
No known key found for this signature in database
GPG Key ID: BB36BF6584A298FF
3 changed files with 37 additions and 2 deletions

View File

@ -371,6 +371,11 @@ class LoggingPlugin(object):
formatter=self.formatter, level=self.log_level) as log_handler: formatter=self.formatter, level=self.log_level) as log_handler:
if self.log_cli_handler: if self.log_cli_handler:
self.log_cli_handler.set_when(when) self.log_cli_handler.set_when(when)
if item is None:
yield # run the test
return
if not hasattr(item, 'catch_log_handlers'): if not hasattr(item, 'catch_log_handlers'):
item.catch_log_handlers = {} item.catch_log_handlers = {}
item.catch_log_handlers[when] = log_handler item.catch_log_handlers[when] = log_handler
@ -402,9 +407,17 @@ class LoggingPlugin(object):
with self._runtest_for(item, 'teardown'): with self._runtest_for(item, 'teardown'):
yield yield
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_logstart(self): def pytest_runtest_logstart(self):
if self.log_cli_handler: if self.log_cli_handler:
self.log_cli_handler.reset() self.log_cli_handler.reset()
with self._runtest_for(None, 'start'):
yield
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_logfinish(self):
with self._runtest_for(None, 'finish'):
yield
@pytest.hookimpl(hookwrapper=True) @pytest.hookimpl(hookwrapper=True)
def pytest_runtestloop(self, session): def pytest_runtestloop(self, session):
@ -474,7 +487,7 @@ class _LiveLoggingStreamHandler(logging.StreamHandler):
if self.capture_manager is not None: if self.capture_manager is not None:
self.capture_manager.suspend_global_capture() self.capture_manager.suspend_global_capture()
try: try:
if not self._first_record_emitted or self._when == 'teardown': if not self._first_record_emitted or self._when in ('teardown', 'finish'):
self.stream.write('\n') self.stream.write('\n')
self._first_record_emitted = True self._first_record_emitted = True
if not self._section_name_shown: if not self._section_name_shown:

1
changelog/3189.feature Normal file
View File

@ -0,0 +1 @@
Allow the logging plugin, when live logs are enabled, to handle the hooks `pytest_runtest_logstart` and `pytest_runtest_logfinish`.

View File

@ -161,6 +161,7 @@ def test_log_cli_enabled_disabled(testdir, enabled):
if enabled: if enabled:
result.stdout.fnmatch_lines([ result.stdout.fnmatch_lines([
'test_log_cli_enabled_disabled.py::test_log_cli ', 'test_log_cli_enabled_disabled.py::test_log_cli ',
'*-- live log call --*',
'test_log_cli_enabled_disabled.py* CRITICAL critical message logged by test', 'test_log_cli_enabled_disabled.py* CRITICAL critical message logged by test',
'PASSED*', 'PASSED*',
]) ])
@ -226,8 +227,20 @@ def test_log_cli_default_level_multiple_tests(testdir, request):
def test_log_cli_default_level_sections(testdir, request): def test_log_cli_default_level_sections(testdir, request):
"""Check that with live logging enable we are printing the correct headers during setup/call/teardown.""" """Check that with live logging enable we are printing the correct headers during
start/setup/call/teardown/finish."""
filename = request.node.name + '.py' filename = request.node.name + '.py'
testdir.makeconftest('''
import pytest
import logging
def pytest_runtest_logstart():
logging.warning('>>>>> START >>>>>')
def pytest_runtest_logfinish():
logging.warning('<<<<< END <<<<<<<')
''')
testdir.makepyfile(''' testdir.makepyfile('''
import pytest import pytest
import logging import logging
@ -252,6 +265,8 @@ def test_log_cli_default_level_sections(testdir, request):
result = testdir.runpytest() result = testdir.runpytest()
result.stdout.fnmatch_lines([ result.stdout.fnmatch_lines([
'{}::test_log_1 '.format(filename), '{}::test_log_1 '.format(filename),
'*-- live log start --*',
'*WARNING* >>>>> START >>>>>*',
'*-- live log setup --*', '*-- live log setup --*',
'*WARNING*log message from setup of test_log_1*', '*WARNING*log message from setup of test_log_1*',
'*-- live log call --*', '*-- live log call --*',
@ -259,8 +274,12 @@ def test_log_cli_default_level_sections(testdir, request):
'PASSED *50%*', 'PASSED *50%*',
'*-- live log teardown --*', '*-- live log teardown --*',
'*WARNING*log message from teardown of test_log_1*', '*WARNING*log message from teardown of test_log_1*',
'*-- live log finish --*',
'*WARNING* <<<<< END <<<<<<<*',
'{}::test_log_2 '.format(filename), '{}::test_log_2 '.format(filename),
'*-- live log start --*',
'*WARNING* >>>>> START >>>>>*',
'*-- live log setup --*', '*-- live log setup --*',
'*WARNING*log message from setup of test_log_2*', '*WARNING*log message from setup of test_log_2*',
'*-- live log call --*', '*-- live log call --*',
@ -268,6 +287,8 @@ def test_log_cli_default_level_sections(testdir, request):
'PASSED *100%*', 'PASSED *100%*',
'*-- live log teardown --*', '*-- live log teardown --*',
'*WARNING*log message from teardown of test_log_2*', '*WARNING*log message from teardown of test_log_2*',
'*-- live log finish --*',
'*WARNING* <<<<< END <<<<<<<*',
'=* 2 passed in *=', '=* 2 passed in *=',
]) ])