ToB企服应用市场:ToB评测及商务社交产业平台

标题: Pytest之生成allure报告 [打印本页]

作者: 去皮卡多    时间: 2023-3-1 22:55
标题: Pytest之生成allure报告
一、前戏

在之前我们已经学会使用 pytest-html 插件生成 html 格式的测试报告:
  1. 1 # 第一步,安装插件
  2. 2 pip install pytest-html
  3. 3 ​
  4. 4 # 第二步,执行用例时使用 --html 参数
  5. 5 ## main 函数中执行
  6. 6 if __name__ == '__main__':
  7. 7     pytest.main(["-vs", "--html=./report/result.html"])
  8. 8     
  9. 9 ## 使用命令模式执行
  10. 10 pytest -vs --html ./report/result.html
复制代码

很明显报告的效果配不上我们高大上的逼格.......除了 pytest-html 插件,pytest 还可以和 allure 结合,生成更加详细美观的测试报告。
二、allure的使用

  1. pip install allure-pytest
复制代码
  1. 网址:https://github.com/allure-framework/allure2/releases
复制代码




  1. 1 [pytest]
  2. 2 addopts = -vs --alluredir ./tmp
  3. 3 testpaths = .
  4. 4 python_files = test_*.py
  5. 5 python_classes = Test*
  6. 6 python_functions = test
复制代码
这个参数的作用是在用例执行时,会在临时文件夹 tmp 中生成很多 json 文件,这些文件记录了用例执行过程中的相关信息,最后生成报告使用到的数据就是从 json 中获取的。

allure generate ./tmp -o ./report --clean
# 参数详情
# ./tmp:存放临时 json 数据的目录
# -o:表示输出 output
# ./report:测试报告存放目录
# --clean:清空 report 目录中原有的数据
为了方便起见,我们一般会把生成报告的命令直接写在主函数里面:
  1. 1 # 根目录下新建一个 all.py
  2. 2 import pytest
  3. 3 import os
  4. 4 ​
  5. 5 if __name__ == '__main__':
  6. 6     pytest.main()  # 执行项目中所有用例
  7. 7     os.system("allure generate ./tmp -o ./report --clean")  # 生成allure测试报告
复制代码
最后报告会生成在 report 目录下:

 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4