pytest-logger如何停止写入日志文件但附加到日志文件的末尾

时间:2018-06-06 16:52:30

标签: python testing logging pytest fixtures

我是pytest和python的新手。

我有一个问题,就是每当我运行测试并生成日志文件时,它都会写入日志文件但不会附加,这会导致我丢失以前的日志。

我现在正在尝试示例代码。

#conftest.py
#import os

def pytest_logger_config(logger_config):
    logger_config.add_loggers(['foo', 'bar', 'baz'], stdout_level='info')
    logger_config.set_log_option_default('foo,bar')

def pytest_logger_logdirlink(config):
    return os.path.join(os.path.dirname(__file__), 'mylogs')



#test_something.py
import pytest
import logging

foo = logging.getLogger('foo')
bar = logging.getLogger('bar')
baz = logging.getLogger('baz')

@pytest.yield_fixture(scope='session')
def session_thing():
    foo.debug('constructing session thing')
    yield
    foo.debug('destroying session thing')

@pytest.yield_fixture
def testcase_thing():
    foo.debug('constructing testcase thing')
    yield
    foo.debug('destroying testcase thing')

def test_one(session_thing, testcase_thing):
    foo.info('one executes')
    bar.warning('this test does nothing aside from logging')
    baz.info('extra log, rarely read')

def test_two(session_thing, testcase_thing):
    foo.info('two executes')
    bar.warning('neither does this')
    baz.info('extra log, not enabled by default')

任何人都有好的解决方案吗?提前谢谢。

0 个答案:

没有答案