Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose log_cli as a CLI parser option. #3190

Merged
merged 3 commits into from
Feb 8, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions _pytest/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ def __init__(self, config):
self._config = config

# enable verbose output automatically if live logging is enabled
if self._config.getini('log_cli') and not config.getoption('verbose'):
if self._log_cli_enabled() and not config.getoption('verbose'):
# sanity check: terminal reporter should not have been loaded at this point
assert self._config.pluginmanager.get_plugin('terminalreporter') is None
config.option.verbose = 1
Expand All @@ -364,6 +364,13 @@ def __init__(self, config):
# initialized during pytest_runtestloop
self.log_cli_handler = None

def _log_cli_enabled(self):
"""Return True if log_cli should be considered enabled, either explicitly
or because --log-cli-level was given in the command-line.
"""
return self._config.getoption('--log-cli-level') is not None or \
self._config.getini('log_cli')
Copy link
Contributor

@twmr twmr Feb 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thx for introducing a method for this! This improves the readability IMO.


@contextmanager
def _runtest_for(self, item, when):
"""Implements the internals of pytest_runtest_xxx() hook."""
Expand Down Expand Up @@ -438,7 +445,7 @@ def _setup_cli_logging(self):
This must be done right before starting the loop so we can access the terminal reporter plugin.
"""
terminal_reporter = self._config.pluginmanager.get_plugin('terminalreporter')
if self._config.getini('log_cli') and terminal_reporter is not None:
if self._log_cli_enabled() and terminal_reporter is not None:
capture_manager = self._config.pluginmanager.get_plugin('capturemanager')
log_cli_handler = _LiveLoggingStreamHandler(terminal_reporter, capture_manager)
log_cli_format = get_option_ini(self._config, 'log_cli_format', 'log_format')
Expand Down
1 change: 1 addition & 0 deletions changelog/3190.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Passing `--log-cli-level` in the command-line now automatically activates live logging.
42 changes: 42 additions & 0 deletions testing/logging/test_reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,48 @@ def test_log_cli(request):
assert result.ret == 0


@pytest.mark.parametrize('cli_args', ['',
'--log-level=WARNING',
'--log-file-level=WARNING',
'--log-cli-level=WARNING'])
def test_log_cli_auto_enable(testdir, request, cli_args):
"""Check that live logs are enabled if --log-level or --log-cli-level is passed on the CLI.
It should not be auto enabled if the same configs are set on the INI file.
"""
testdir.makepyfile('''
import pytest
import logging

def test_log_1():
logging.info("log message from test_log_1 not to be shown")
logging.warning("log message from test_log_1")

''')
testdir.makeini('''
[pytest]
log_level=INFO
log_cli_level=INFO
''')

result = testdir.runpytest(cli_args)
if cli_args == '--log-cli-level=WARNING':
result.stdout.fnmatch_lines([
'*::test_log_1 ',
'*-- live log call --*',
'*WARNING*log message from test_log_1*',
'PASSED *100%*',
'=* 1 passed in *=',
])
assert 'INFO' not in result.stdout.str()
else:
result.stdout.fnmatch_lines([
'*test_log_cli_auto_enable*100%*',
'=* 1 passed in *=',
])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also ensure that no logging message is shown:

 assert 'INFO' not in result.stdout.str()
 assert 'WARNING' not in result.stdout.str()

assert 'INFO' not in result.stdout.str()
assert 'WARNING' not in result.stdout.str()


def test_log_file_cli(testdir):
# Default log file level
testdir.makepyfile('''
Expand Down