Skip to content

Commit

Permalink
Merge pull request #3188 from s0undt3ch/issues/3184
Browse files Browse the repository at this point in the history
Don't traceback on unkown sections.
  • Loading branch information
nicoddemus authored Feb 17, 2018
2 parents 6bc45d1 + 42c1f85 commit 00d3001
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
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

0 comments on commit 00d3001

Please sign in to comment.