From 0b71255ddaea1b31c52c52454e0adc81a395577c Mon Sep 17 00:00:00 2001 From: Pedro Algarvio Date: Mon, 5 Feb 2018 18:07:40 +0000 Subject: [PATCH 1/3] Expose `log_cli` as a CLI parser option. --- _pytest/logging.py | 11 ++++++-- changelog/3190.feature | 1 + testing/logging/test_reporting.py | 42 +++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 changelog/3190.feature diff --git a/_pytest/logging.py b/_pytest/logging.py index 2de7786eae4..685c2831e96 100644 --- a/_pytest/logging.py +++ b/_pytest/logging.py @@ -336,9 +336,10 @@ def __init__(self, config): create a single one for the entire test session here. """ self._config = config + self._stream_logs = None # enable verbose output automatically if live logging is enabled - if self._config.getini('log_cli') and not config.getoption('verbose'): + if self._stream_logs_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 @@ -364,6 +365,12 @@ def __init__(self, config): # initialized during pytest_runtestloop self.log_cli_handler = None + def _stream_logs_enabled(self): + if self._stream_logs is None: + self._stream_logs = self._config.getoption('--log-cli-level') is not None or \ + self._config.getini('log_cli') + return self._stream_logs + @contextmanager def _runtest_for(self, item, when): """Implements the internals of pytest_runtest_xxx() hook.""" @@ -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._stream_logs_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') diff --git a/changelog/3190.feature b/changelog/3190.feature new file mode 100644 index 00000000000..95bb5e39be4 --- /dev/null +++ b/changelog/3190.feature @@ -0,0 +1 @@ +Passing `--log-cli-level` in the command-line now automatically activates live logging. diff --git a/testing/logging/test_reporting.py b/testing/logging/test_reporting.py index 40e2cdbc530..d7ba63a4ea4 100644 --- a/testing/logging/test_reporting.py +++ b/testing/logging/test_reporting.py @@ -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 *=', + ]) + 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(''' From ad7d63df97f60e23a26424ac04f515957e998531 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Thu, 8 Feb 2018 09:48:51 -0200 Subject: [PATCH 2/3] Rename _stream_logs_enabled to _log_cli_enabled and remove _stream_logs --- _pytest/logging.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/_pytest/logging.py b/_pytest/logging.py index 685c2831e96..fd90e9c949f 100644 --- a/_pytest/logging.py +++ b/_pytest/logging.py @@ -336,10 +336,9 @@ def __init__(self, config): create a single one for the entire test session here. """ self._config = config - self._stream_logs = None # enable verbose output automatically if live logging is enabled - if self._stream_logs_enabled() 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 @@ -365,11 +364,12 @@ def __init__(self, config): # initialized during pytest_runtestloop self.log_cli_handler = None - def _stream_logs_enabled(self): - if self._stream_logs is None: - self._stream_logs = self._config.getoption('--log-cli-level') is not None or \ - self._config.getini('log_cli') - return self._stream_logs + 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') @contextmanager def _runtest_for(self, item, when): @@ -445,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._stream_logs_enabled() 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') From a4cbd035359f1ac01924b0b05cf93bb1c877cdc6 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Thu, 8 Feb 2018 13:22:32 -0200 Subject: [PATCH 3/3] Fix linting --- _pytest/logging.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_pytest/logging.py b/_pytest/logging.py index fd90e9c949f..ecb992cead6 100644 --- a/_pytest/logging.py +++ b/_pytest/logging.py @@ -369,7 +369,7 @@ def _log_cli_enabled(self): 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') + self._config.getini('log_cli') @contextmanager def _runtest_for(self, item, when):