From d6f75d28364b4434217b45b261573f07569f5cd8 Mon Sep 17 00:00:00 2001 From: Thomas Hisch Date: Sun, 17 Dec 2017 20:11:24 +0100 Subject: [PATCH] WIP --- _pytest/logging.py | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/_pytest/logging.py b/_pytest/logging.py index 071387902..8e0a2dcc0 100644 --- a/_pytest/logging.py +++ b/_pytest/logging.py @@ -5,6 +5,7 @@ from contextlib import closing, contextmanager import sys import six +import _pytest import pytest import py @@ -120,14 +121,15 @@ class LogCaptureHandler(logging.StreamHandler): logging.StreamHandler.emit(self, record) -class TerminalWriterHandler(logging.Handler): - def __init__(self): - # TODO sys.stderr is captured by default ?!??! - self.tw = py.io.TerminalWriter(sys.stderr) - super(TerminalWriterHandler, self).__init__() +class TerminalReporterHandler(logging.Handler): + def __init__(self, reporter): + self.reporter = reporter + super(TerminalReporterHandler, self).__init__() def emit(self, record): - self.tw.write(self.format(record)) + # FIXME: I don't get any output with the reporter obj + # self.reporter.write(self.format(record)) + print(self.format(record)) class LogCaptureFixture(object): @@ -264,7 +266,6 @@ class LoggingPlugin(object): get_option_ini(config, 'log_format'), get_option_ini(config, 'log_date_format')) - log_cli_format = get_option_ini( config, 'log_cli_format', 'log_format') log_cli_date_format = get_option_ini( @@ -273,6 +274,8 @@ class LoggingPlugin(object): log_cli_format, datefmt=log_cli_date_format) + self.reporter = _pytest.config.create_terminal_writer(config, sys.stdout) + log_file = get_option_ini(config, 'log_file') if log_file: self.log_file_level = get_actual_log_level( @@ -296,6 +299,10 @@ class LoggingPlugin(object): @contextmanager def _runtest_for(self, item, when): """Implements the internals of pytest_runtest_xxx() hook.""" + if self.capture_log != 'on-failure': + yield + return + with catching_logs(LogCaptureHandler(), formatter=self.formatter) as log_handler: item.catch_log_handler = log_handler @@ -328,9 +335,8 @@ class LoggingPlugin(object): def pytest_runtestloop(self, session): """Runs all collected test items.""" - # TODO what should happen at the end of the tests? if self.capture_log == 'live': - with catching_logs(TerminalWriterHandler(), + with catching_logs(TerminalReporterHandler(self.reporter), formatter=self.log_cli_formatter, level=self.log_cli_level): if self.log_file_handler is not None: