Skip to content

Don't traceback on unkown sections. #3188

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

Merged
merged 2 commits into from
Feb 17, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion _pytest/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ def emit(self, record):
if not self._first_record_emitted or self._when == 'teardown':
self.stream.write('\n')
self._first_record_emitted = True
if not self._section_name_shown:
if not self._section_name_shown and self._when:
self.stream.section('live log ' + self._when, sep='-', bold=True)
self._section_name_shown = True
logging.StreamHandler.emit(self, record)
Expand Down
1 change: 1 addition & 0 deletions changelog/3184.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix bug where logging happening at hooks outside of "test run" hooks would cause an internal error.
54 changes: 54 additions & 0 deletions testing/logging/test_reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,60 @@ def test_log_2(fix):
])


def test_live_logs_unknown_sections(testdir, request):
"""Check that with live logging enable we are printing the correct headers during
start/setup/call/teardown/finish."""
filename = request.node.name + '.py'
testdir.makeconftest('''
import pytest
import logging

def pytest_runtest_protocol(item, nextitem):
logging.warning('Unknown Section!')

def pytest_runtest_logstart():
logging.warning('>>>>> START >>>>>')

def pytest_runtest_logfinish():
logging.warning('<<<<< END <<<<<<<')
''')

testdir.makepyfile('''
import pytest
import logging

@pytest.fixture
def fix(request):
logging.warning("log message from setup of {}".format(request.node.name))
yield
logging.warning("log message from teardown of {}".format(request.node.name))

def test_log_1(fix):
logging.warning("log message from test_log_1")

''')
testdir.makeini('''
[pytest]
log_cli=true
''')

result = testdir.runpytest()
result.stdout.fnmatch_lines([
'*WARNING*Unknown Section*',
'{}::test_log_1 '.format(filename),
'*WARNING* >>>>> START >>>>>*',
'*-- live log setup --*',
'*WARNING*log message from setup of test_log_1*',
'*-- live log call --*',
'*WARNING*log message from test_log_1*',
'PASSED *100%*',
'*-- live log teardown --*',
'*WARNING*log message from teardown of test_log_1*',
'*WARNING* <<<<< END <<<<<<<*',
'=* 1 passed in *=',
])


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