From acda6c46fb188514738bbb90f17279c11daa0510 Mon Sep 17 00:00:00 2001 From: Thomas Hisch Date: Mon, 19 Feb 2018 20:34:11 +0100 Subject: [PATCH] Partially revert "Remove --no-print-logs option" We'll deprecate --no-print-logs beginning with pytest-4.0. This reverts commit ac7eb63a6b9899250ebd58f61f348ef69bf55875. --- _pytest/logging.py | 13 ++++++-- testing/logging/test_reporting.py | 54 +++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 3 deletions(-) diff --git a/_pytest/logging.py b/_pytest/logging.py index b719f3a79..d3ac81e65 100644 --- a/_pytest/logging.py +++ b/_pytest/logging.py @@ -84,6 +84,11 @@ def pytest_addoption(parser): help='default value for ' + option) group.addoption(option, dest=dest, **kwargs) + add_option_ini( + '--no-print-logs', + dest='log_print', action='store_const', const=False, default=True, + type='bool', + help='disable printing caught logs on failed tests.') add_option_ini( '--log-level', dest='log_level', default=None, @@ -338,6 +343,7 @@ class LoggingPlugin(object): assert self._config.pluginmanager.get_plugin('terminalreporter') is None config.option.verbose = 1 + self.print_logs = get_option_ini(config, 'log_print') self.formatter = logging.Formatter(get_option_ini(config, 'log_format'), get_option_ini(config, 'log_date_format')) self.log_level = get_actual_log_level(config, 'log_level') @@ -388,9 +394,10 @@ class LoggingPlugin(object): if when == 'teardown': del item.catch_log_handlers - # Add a captured log section to the report. - log = log_handler.stream.getvalue().strip() - item.add_report_section(when, 'log', log) + if self.print_logs: + # Add a captured log section to the report. + log = log_handler.stream.getvalue().strip() + item.add_report_section(when, 'log', log) @pytest.hookimpl(hookwrapper=True) def pytest_runtest_setup(self, item): diff --git a/testing/logging/test_reporting.py b/testing/logging/test_reporting.py index 7f4c3f17d..f84f7e459 100644 --- a/testing/logging/test_reporting.py +++ b/testing/logging/test_reporting.py @@ -91,6 +91,60 @@ def test_teardown_logging(testdir): '*text going to logger from teardown*']) +def test_disable_log_capturing(testdir): + testdir.makepyfile(''' + import sys + import logging + + logger = logging.getLogger(__name__) + + def test_foo(): + sys.stdout.write('text going to stdout') + logger.warning('catch me if you can!') + sys.stderr.write('text going to stderr') + assert False + ''') + result = testdir.runpytest('--no-print-logs') + print(result.stdout) + assert result.ret == 1 + result.stdout.fnmatch_lines(['*- Captured stdout call -*', + 'text going to stdout']) + result.stdout.fnmatch_lines(['*- Captured stderr call -*', + 'text going to stderr']) + with pytest.raises(pytest.fail.Exception): + result.stdout.fnmatch_lines(['*- Captured *log call -*']) + + +def test_disable_log_capturing_ini(testdir): + testdir.makeini( + ''' + [pytest] + log_print=False + ''' + ) + testdir.makepyfile(''' + import sys + import logging + + logger = logging.getLogger(__name__) + + def test_foo(): + sys.stdout.write('text going to stdout') + logger.warning('catch me if you can!') + sys.stderr.write('text going to stderr') + assert False + ''') + result = testdir.runpytest() + print(result.stdout) + assert result.ret == 1 + result.stdout.fnmatch_lines(['*- Captured stdout call -*', + 'text going to stdout']) + result.stdout.fnmatch_lines(['*- Captured stderr call -*', + 'text going to stderr']) + with pytest.raises(pytest.fail.Exception): + 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'