Skip to content

Commit 05faa69

Browse files
authored
Merge pull request #3194 from s0undt3ch/feature/logstart-logfinish
Fix issue where a new line was always written for the live log finish section
2 parents b1abe5d + d776e56 commit 05faa69

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

_pytest/logging.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,7 @@ def __init__(self, terminal_reporter, capture_manager):
480480
self.capture_manager = capture_manager
481481
self.reset()
482482
self.set_when(None)
483+
self._test_outcome_written = False
483484

484485
def reset(self):
485486
"""Reset the handler; should be called before the start of each test"""
@@ -489,14 +490,20 @@ def set_when(self, when):
489490
"""Prepares for the given test phase (setup/call/teardown)"""
490491
self._when = when
491492
self._section_name_shown = False
493+
if when == 'start':
494+
self._test_outcome_written = False
492495

493496
def emit(self, record):
494497
if self.capture_manager is not None:
495498
self.capture_manager.suspend_global_capture()
496499
try:
497-
if not self._first_record_emitted or self._when in ('teardown', 'finish'):
500+
if not self._first_record_emitted:
498501
self.stream.write('\n')
499502
self._first_record_emitted = True
503+
elif self._when in ('teardown', 'finish'):
504+
if not self._test_outcome_written:
505+
self._test_outcome_written = True
506+
self.stream.write('\n')
500507
if not self._section_name_shown:
501508
self.stream.section('live log ' + self._when, sep='-', bold=True)
502509
self._section_name_shown = True

testing/logging/test_reporting.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# -*- coding: utf-8 -*-
2+
import re
23
import os
34

45
import six
@@ -293,6 +294,64 @@ def test_log_2(fix):
293294
])
294295

295296

297+
def test_sections_single_new_line_after_test_outcome(testdir, request):
298+
"""Check that only a single new line is written between log messages during
299+
teardown/finish."""
300+
filename = request.node.name + '.py'
301+
testdir.makeconftest('''
302+
import pytest
303+
import logging
304+
305+
def pytest_runtest_logstart():
306+
logging.warning('>>>>> START >>>>>')
307+
308+
def pytest_runtest_logfinish():
309+
logging.warning('<<<<< END <<<<<<<')
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+
logging.warning("log message from teardown of {}".format(request.node.name))
323+
324+
def test_log_1(fix):
325+
logging.warning("log message from test_log_1")
326+
''')
327+
testdir.makeini('''
328+
[pytest]
329+
log_cli=true
330+
''')
331+
332+
result = testdir.runpytest()
333+
result.stdout.fnmatch_lines([
334+
'{}::test_log_1 '.format(filename),
335+
'*-- live log start --*',
336+
'*WARNING* >>>>> START >>>>>*',
337+
'*-- live log setup --*',
338+
'*WARNING*log message from setup of test_log_1*',
339+
'*-- live log call --*',
340+
'*WARNING*log message from test_log_1*',
341+
'PASSED *100%*',
342+
'*-- live log teardown --*',
343+
'*WARNING*log message from teardown of test_log_1*',
344+
'*-- live log finish --*',
345+
'*WARNING* <<<<< END <<<<<<<*',
346+
'*WARNING* <<<<< END <<<<<<<*',
347+
'=* 1 passed in *=',
348+
])
349+
assert re.search(r'(.+)live log teardown(.+)\n(.+)WARNING(.+)\n(.+)WARNING(.+)',
350+
result.stdout.str(), re.MULTILINE) is not None
351+
assert re.search(r'(.+)live log finish(.+)\n(.+)WARNING(.+)\n(.+)WARNING(.+)',
352+
result.stdout.str(), re.MULTILINE) is not None
353+
354+
296355
def test_log_cli_level(testdir):
297356
# Default log file level
298357
testdir.makepyfile('''

0 commit comments

Comments
 (0)