Skip to content

Commit 53eaafe

Browse files
committed
Don't traceback on unkown sections.
1 parent 1aa25ec commit 53eaafe

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

_pytest/logging.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ def emit(self, record):
487487
if not self._first_record_emitted or self._when in ('teardown', 'finish'):
488488
self.stream.write('\n')
489489
self._first_record_emitted = True
490-
if not self._section_name_shown:
490+
if not self._section_name_shown and self._when:
491491
self.stream.section('live log ' + self._when, sep='-', bold=True)
492492
self._section_name_shown = True
493493
logging.StreamHandler.emit(self, record)

changelog/3175.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Don't traceback when logging on unknown(unhandled) sections.

testing/logging/test_reporting.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,62 @@ def test_log_2(fix):
292292
])
293293

294294

295+
def test_live_logs_unknown_sections(testdir, request):
296+
"""Check that with live logging enable we are printing the correct headers during
297+
start/setup/call/teardown/finish."""
298+
filename = request.node.name + '.py'
299+
testdir.makeconftest('''
300+
import pytest
301+
import logging
302+
303+
def pytest_runtest_protocol(item, nextitem):
304+
logging.warning('Unknown Section!')
305+
306+
def pytest_runtest_logstart():
307+
logging.warning('>>>>> START >>>>>')
308+
309+
def pytest_runtest_logfinish():
310+
logging.warning('<<<<< END <<<<<<<')
311+
''')
312+
313+
testdir.makepyfile('''
314+
import pytest
315+
import logging
316+
317+
@pytest.fixture
318+
def fix(request):
319+
logging.warning("log message from setup of {}".format(request.node.name))
320+
yield
321+
logging.warning("log message from teardown of {}".format(request.node.name))
322+
323+
def test_log_1(fix):
324+
logging.warning("log message from test_log_1")
325+
326+
''')
327+
testdir.makeini('''
328+
[pytest]
329+
log_cli=true
330+
''')
331+
332+
result = testdir.runpytest()
333+
result.stdout.fnmatch_lines([
334+
'*WARNING*Unknown Section*',
335+
'{}::test_log_1 '.format(filename),
336+
'*-- live log start --*',
337+
'*WARNING* >>>>> START >>>>>*',
338+
'*-- live log setup --*',
339+
'*WARNING*log message from setup of test_log_1*',
340+
'*-- live log call --*',
341+
'*WARNING*log message from test_log_1*',
342+
'PASSED *100%*',
343+
'*-- live log teardown --*',
344+
'*WARNING*log message from teardown of test_log_1*',
345+
'*-- live log finish --*',
346+
'*WARNING* <<<<< END <<<<<<<*',
347+
'=* 1 passed in *=',
348+
])
349+
350+
295351
def test_log_cli_level(testdir):
296352
# Default log file level
297353
testdir.makepyfile('''

0 commit comments

Comments
 (0)