在项目目录下配置pytest.ini 和conftest.py
![在这里插入图片描述](https://img-blog.csdnimg.cn/df850dd0f1ce49a4bcb9316a7cae36f9.png)
pytest.ini
[pytest]
log_cli = 1
log_cli_level = INFO
log_cli_date_format = %Y-%m-%d %H:%M:%S
log_cli_format = %(asctime)s -%(levelname)s %(filename)s->%(lineno)d : %(message)s
conftest.py
import pytest
import logging
import allure
class AllureLoggingHandler(logging.Handler):
def log(self, message):
with allure.step('{}'.format(message)):
pass
def emit(self, record):
self.log("{}-{}-: {}".format(record.asctime, record.levelname, record.getMessage()))
class AllureCatchLogs:
def __init__(self):
self.rootlogger = logging.getLogger()
self.allurehandler = AllureLoggingHandler()
def __enter__(self):
if self.allurehandler not in self.rootlogger.handlers:
self.rootlogger.addHandler(self.allurehandler)
def __exit__(self, exc_type, exc_value, traceback):
self.rootlogger.removeHandler(self.allurehandler)
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_setup():
with AllureCatchLogs():
yield
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_call():
with AllureCatchLogs():
yield
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_teardown():
with AllureCatchLogs():
yield