一、Allure 的简介和使用
帮助文档
地址:https://docs.qameta.io/allure/
1.1.alure报告生成步骤
最终我们会生成一个 html 格式的报告,中间我们需要操作两步来进行。
- 生成xml
- 将 xml 转成 html
安装
pip install allure-pytest
使用步骤
- 将 pytest 配置文件中的命令行参数加上如下代码
--alluredir report
[pytest]
# 添加行参数
addopts = -s --alluredir report
# 文件搜索路径
testpaths = ./scripts
# 文件名称
python_files = test_*.py
# 类名称
python_classes = Test*
# 方法名称
python_functions = test_*
2.编写好测试脚本后,正常的在命令行中运行 pytest 即可。
3.程序运行结束后,我们会在项目中得到一个json文件
1.2.将 xml 转成 html
- https://bintray.com/qameta/generic/allure2 下载 allure-2.6.0.zip
- 解压缩到一个目录(不经常动的目录)
- 将压缩包内的 bin 目录配置到 path 系统环境变量
- 在命令行中敲 allure 命令,如果提示有这个命令,即为成功
使用步骤
在保证项目中的 report 目录下有 xml 文件的时候,执行以下步骤。
- 进入 report 上级目录执行命令
allure generate report/ -o report/html --clean
- report 目录下会生成 html 文件夹,html 下会有一个 index.html ,右键用浏览器打开即可。
1.3 参数和命令详解
应用场景
修改 xml 所在的目录名称和 index.html 所在的目录名称
疑问和解答
- addopts = -s --alluredir report 中的 --alluredir report 是什么意思?
–alluredir 后面的 report 为 xml 输出的目录名
如果希望目录名叫 result 那么可以将命令行参数改为 --alluredir result - allure generate report/ -o report/html --clean 是什么意思?
report/ 表示 xml 所在的目录
-o 表示 output 输出
report/html 表示将 index.html 报告生成到哪个文件夹
二、Allure 与 pytest 结合
2.1添加测试步骤
应用场景
一套登录流程需要至少三个步骤,输入用户名,输入密码,点击登录。我们可以通过添加测试步骤,让这些步骤在报告中进行体现
使用方式
在 page 中的所有方法上加上 @allure.step(title=“测试步骤001”) 装饰器即可
核心代码
page/setting_page.py
@allure.step(title="点击搜索")
def click_search(self):
self.click(