log_cli must now be enabled explicitly

Ref: #3013
This commit is contained in:
Bruno Oliveira 2018-01-15 19:02:34 -02:00
parent 3b3d237f07
commit 5ad1313b8a
2 changed files with 57 additions and 18 deletions

View File

@ -48,6 +48,9 @@ def pytest_addoption(parser):
'--log-date-format', '--log-date-format',
dest='log_date_format', default=DEFAULT_LOG_DATE_FORMAT, dest='log_date_format', default=DEFAULT_LOG_DATE_FORMAT,
help='log date format as used by the logging module.') help='log date format as used by the logging module.')
parser.addini(
'log_cli', default=False, type='bool',
help='enable log display during test run (also known as "live logging").')
add_option_ini( add_option_ini(
'--log-cli-level', '--log-cli-level',
dest='log_cli_level', default=None, dest='log_cli_level', default=None,
@ -234,8 +237,12 @@ def get_actual_log_level(config, *setting_names):
def pytest_configure(config): def pytest_configure(config):
config.pluginmanager.register(LoggingPlugin(config), config.pluginmanager.register(LoggingPlugin(config), 'logging-plugin')
'logging-plugin')
@contextmanager
def _dummy_context_manager():
yield
class LoggingPlugin(object): class LoggingPlugin(object):
@ -248,14 +255,12 @@ class LoggingPlugin(object):
The formatter can be safely shared across all handlers so The formatter can be safely shared across all handlers so
create a single one for the entire test session here. create a single one for the entire test session here.
""" """
self.log_cli_level = get_actual_log_level(
config, 'log_cli_level', 'log_level') or logging.WARNING
self.print_logs = get_option_ini(config, 'log_print') self.print_logs = get_option_ini(config, 'log_print')
self.formatter = logging.Formatter( self.formatter = logging.Formatter(
get_option_ini(config, 'log_format'), get_option_ini(config, 'log_format'),
get_option_ini(config, 'log_date_format')) get_option_ini(config, 'log_date_format'))
if config.getini('log_cli'):
log_cli_handler = logging.StreamHandler(sys.stderr) log_cli_handler = logging.StreamHandler(sys.stderr)
log_cli_format = get_option_ini( log_cli_format = get_option_ini(
config, 'log_cli_format', 'log_format') config, 'log_cli_format', 'log_format')
@ -264,10 +269,15 @@ class LoggingPlugin(object):
log_cli_formatter = logging.Formatter( log_cli_formatter = logging.Formatter(
log_cli_format, log_cli_format,
datefmt=log_cli_date_format) datefmt=log_cli_date_format)
log_cli_level = get_actual_log_level(
config, 'log_cli_level', 'log_level') or logging.WARNING
self.log_cli_handler = log_cli_handler # needed for a single unittest self.log_cli_handler = log_cli_handler # needed for a single unittest
self.live_logs = catching_logs(log_cli_handler, self.live_logs_context = catching_logs(log_cli_handler,
formatter=log_cli_formatter, formatter=log_cli_formatter,
level=self.log_cli_level) level=log_cli_level)
else:
self.log_cli_handler = None
self.live_logs_context = _dummy_context_manager()
log_file = get_option_ini(config, 'log_file') log_file = get_option_ini(config, 'log_file')
if log_file: if log_file:
@ -328,7 +338,7 @@ class LoggingPlugin(object):
@pytest.hookimpl(hookwrapper=True) @pytest.hookimpl(hookwrapper=True)
def pytest_runtestloop(self, session): def pytest_runtestloop(self, session):
"""Runs all collected test items.""" """Runs all collected test items."""
with self.live_logs: with self.live_logs_context:
if self.log_file_handler is not None: if self.log_file_handler is not None:
with closing(self.log_file_handler): with closing(self.log_file_handler):
with catching_logs(self.log_file_handler, with catching_logs(self.log_file_handler,

View File

@ -141,6 +141,26 @@ def test_disable_log_capturing_ini(testdir):
result.stdout.fnmatch_lines(['*- Captured *log call -*']) result.stdout.fnmatch_lines(['*- Captured *log call -*'])
@pytest.mark.parametrize('enabled', [True, False])
def test_log_cli_enabled_disabled(testdir, enabled):
msg = 'critical message logged by test'
testdir.makepyfile('''
import logging
def test_log_cli():
logging.critical("{}")
'''.format(msg))
if enabled:
testdir.makeini('''
[pytest]
log_cli=true
''')
result = testdir.runpytest('-s')
if enabled:
assert msg in result.stderr.str()
else:
assert msg not in result.stderr.str()
def test_log_cli_default_level(testdir): def test_log_cli_default_level(testdir):
# Default log file level # Default log file level
testdir.makepyfile(''' testdir.makepyfile('''
@ -153,6 +173,10 @@ def test_log_cli_default_level(testdir):
logging.getLogger('catchlog').warning("This log message will be shown") logging.getLogger('catchlog').warning("This log message will be shown")
print('PASSED') print('PASSED')
''') ''')
testdir.makeini('''
[pytest]
log_cli=true
''')
result = testdir.runpytest('-s') result = testdir.runpytest('-s')
@ -186,6 +210,10 @@ def test_log_cli_level(testdir):
logging.getLogger('catchlog').info("This log message will be shown") logging.getLogger('catchlog').info("This log message will be shown")
print('PASSED') print('PASSED')
''') ''')
testdir.makeini('''
[pytest]
log_cli=true
''')
result = testdir.runpytest('-s', '--log-cli-level=INFO') result = testdir.runpytest('-s', '--log-cli-level=INFO')
@ -230,6 +258,7 @@ def test_log_cli_ini_level(testdir):
testdir.makeini( testdir.makeini(
""" """
[pytest] [pytest]
log_cli=true
log_cli_level = INFO log_cli_level = INFO
""") """)
testdir.makepyfile(''' testdir.makepyfile('''