使用pytest时生成allure报告会觉得很麻烦,不像pytest-html插件一样,两步就能生成html报告。
pip install pytest-html
pytest --html report/report.html
allure的话就要这样
pip install allure-pytest
pytest --alluredir allure-results
然后allure-results文件夹只有一堆xml文件,这时还需要安装allure-command-tools来解析生成html报告,
如> allure generate allure-results -o allure-report
之后就能在allure-report中发现html的测试报告了。
相比于pytest-html多了一个步骤,而且装的allure版本和pytest可能有匹配不上的问题,具体执行时会有一个找不到pytest-allure hook报错。
也可以利用pytest的hook函数,实现测试完成后自动生成并打开allure报告。
# conftest.py
def pytest_sessionfinish(session):
"""测试完成自动生成并打开allure报告"""
if session.config.getoption('allure_report_dir'):
try:
# 判断allure在环境路径中,通常意味着可以直接执行
if [i for i in os.getenv('path').split(';') if os.path.exists(i) and 'allure' in os.listdir(i)]:
# 默认生成报告路径为: ./allure-report
os.system(f"allure generate -c {session.config.getoption('allure_report_dir')}")
os.system(f"allure open allure-report")
else:
logger.warn('allure不在环境变量中,无法直接生成html报告!')
except Exception as e:
logger.warn(e)
Allure报告功能强大太多了,比如错误分类,case的历史执行状态等。它的Jenkins插件倒是方便许多,自动执行了allure generate命令,配置一下每次生成报告的路径,就可以直接查看啦。