一、环境搭建
- 下载allure,并配置环境变量Path(allure路径C:\allure-2.17.2\allure-2.17.2\bin)
Releases · allure-framework/allure2 (github.com) - PyCharm项目环境安装allure-pytest包
二、生成报告
首先,设置allure results存放目录:
pytest.ini
addopts = --alluredir=./tmp --clean-alluredir
然后,从json结果生成html样式报告:
main.py
import datetime
import os
import pytest
def timestamp(fmt) -> str:
"""
:param fmt: 例如"%Y%m%d%H%M%S"
:return: 时间戳
"""
return datetime.datetime.now().strftime(fmt)
if __name__ == '__main__':
pytest.main()
report_path = "./reports/report_%s" % timestamp("%Y%m%d%H%M%S")
os.system(f"allure generate ./tmp -o {report_path}")
最后,运行main
三、allure报告常用定制
@allure.epic("项目名称")
@allure.feature("功能模块名称")
@allure.story("接口名称:xx接口")
@allure.title("用例标题:xx接口成功正例")
@allure.description("用例描述")
@allure.severity(allure.severity_level.NORMAL) 优先级
以上除了epic,也可以动态设置allure.dynamic.xxx
with allure.step(title) 增加用例执行步骤
allure.attach() 增加截图(web自动化)或文本(比如请求要素)