我是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')
任何人都有好的解决方案吗?提前谢谢。