Skip to content

Commit

Permalink
Partially revert "Remove --no-print-logs option"
Browse files Browse the repository at this point in the history
We'll deprecate --no-print-logs beginning with pytest-4.0.

This reverts commit ac7eb63.
  • Loading branch information
twmr committed Feb 19, 2018
1 parent ac7eb63 commit acda6c4
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 3 deletions.
13 changes: 10 additions & 3 deletions _pytest/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ def add_option_ini(option, dest, default=None, type=None, **kwargs):
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,
Expand Down Expand Up @@ -338,6 +343,7 @@ def __init__(self, config):
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')
Expand Down Expand Up @@ -388,9 +394,10 @@ def _runtest_for(self, item, when):
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):
Expand Down
54 changes: 54 additions & 0 deletions testing/logging/test_reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,60 @@ def teardown_function(function):
'*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'
Expand Down

0 comments on commit acda6c4

Please sign in to comment.