我猜您在pytest.ini中缺少的是log_cli = 1(或true / yes / etc).除此之外,通过配置为您提供的日志记录以您在log_cli_format中指定的格式打印.您甚至可以将pytest.ini减少为:
[pytest]
log_cli = 1
log_cli_level = INFO
log_cli_format = %(asctime)s %(levelname)s %(message)s
log_cli_date_format = %H:%M:%S
另外,上述配置将在测试会话中处理根记录器配置,因此您无需在测试中配置记录器以进行实时记录.只需在测试中调用记录器即可:
import logging
def test_spam():
logger = logging.getLogger(__name__)
logger.info('spam')
logger.warning('eggs')
logger.error('bacon')
这将打印:
$pytest
============================== test session starts ============================