Skip to content

Commit 00d3001

Browse files
authored
Merge pull request #3188 from s0undt3ch/issues/3184
Don't traceback on unkown sections.
2 parents 6bc45d1 + 42c1f85 commit 00d3001

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

_pytest/logging.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ def emit(self, record):
477477
if not self._first_record_emitted or self._when == 'teardown':
478478
self.stream.write('\n')
479479
self._first_record_emitted = True
480-
if not self._section_name_shown:
480+
if not self._section_name_shown and self._when:
481481
self.stream.section('live log ' + self._when, sep='-', bold=True)
482482
self._section_name_shown = True
483483
logging.StreamHandler.emit(self, record)

changelog/3184.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix bug where logging happening at hooks outside of "test run" hooks would cause an internal error.

testing/logging/test_reporting.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,60 @@ def test_log_2(fix):
272272
])
273273

274274

275+
def test_live_logs_unknown_sections(testdir, request):
276+
"""Check that with live logging enable we are printing the correct headers during
277+
start/setup/call/teardown/finish."""
278+
filename = request.node.name + '.py'
279+
testdir.makeconftest('''
280+
import pytest
281+
import logging
282+
283+
def pytest_runtest_protocol(item, nextitem):
284+
logging.warning('Unknown Section!')
285+
286+
def pytest_runtest_logstart():
287+
logging.warning('>>>>> START >>>>>')
288+
289+
def pytest_runtest_logfinish():
290+
logging.warning('<<<<< END <<<<<<<')
291+
''')
292+
293+
testdir.makepyfile('''
294+
import pytest
295+
import logging
296+
297+
@pytest.fixture
298+
def fix(request):
299+
logging.warning("log message from setup of {}".format(request.node.name))
300+
yield
301+
logging.warning("log message from teardown of {}".format(request.node.name))
302+
303+
def test_log_1(fix):
304+
logging.warning("log message from test_log_1")
305+
306+
''')
307+
testdir.makeini('''
308+
[pytest]
309+
log_cli=true
310+
''')
311+
312+
result = testdir.runpytest()
313+
result.stdout.fnmatch_lines([
314+
'*WARNING*Unknown Section*',
315+
'{}::test_log_1 '.format(filename),
316+
'*WARNING* >>>>> START >>>>>*',
317+
'*-- live log setup --*',
318+
'*WARNING*log message from setup of test_log_1*',
319+
'*-- live log call --*',
320+
'*WARNING*log message from test_log_1*',
321+
'PASSED *100%*',
322+
'*-- live log teardown --*',
323+
'*WARNING*log message from teardown of test_log_1*',
324+
'*WARNING* <<<<< END <<<<<<<*',
325+
'=* 1 passed in *=',
326+
])
327+
328+
275329
def test_log_cli_level(testdir):
276330
# Default log file level
277331
testdir.makepyfile('''

0 commit comments

Comments
 (0)