广深小龙最近有写 pytest + request + yaml + allure...的测试框架,认为总体还不错。pytest 单元测试框架需配合 allure 生成漂亮的测试报告。
1.首先需要去github上下载最新版进行安装:https://github.com/allure-framework/allure2/releases
2.allure 生成报告前提需安装好 pytest 与 allure-pytest :pip install pytest allure-pytest
pip install pytest allure-pytest
3.--alluredir 指定生成的json文件存放目录,如:pytest --alluredir ../report/allure_raw
4.实时生成报告命令:allure serve json文件目录,运行命令后自动弹出web浏览器查看报告(右下角可以切换网页语言),如下:
G:\demo\pytest_demo\api_package\cases>allure serve ../report/allure_raw Generating report to temp directory... Report successfully generated to C:\Users\ADMINI~1\AppData\Local\Temp\7463144076250537920\allure-report Starting web server... 2020-05-10 10:18:14.573:INFO::main: Logging initialized @3810ms to org.eclipse.jetty.util.log.StdErrLog Server started at <http://169.254.16.95:7885/>. Press <Ctrl+C> to exit
为什么我的allure报告会没有数据?
显然是 allure serve 生成json文件目录不对应,所以要检查下目录是否正确。
1.命令:allure generate json文件目录 html生成目录 (html目录没有会自动生成)
G:\demo\pytest_demo\api_package\cases>allure generate ../report/allure_raw -o ../report/html/
Report successfully generated to ..\report\html
2.生成html后,右键 index.html 文件打开 Open in Browser 选择一个浏览器打开。
3.你会发现我直接在桌面用一般浏览器包括 Chrom 打开显示没有数据,显示Loading...
这是因为 html 导入css、js是相对路径浏览器识别不出来。
最后解决思路(linux上持续集成,推荐第3):
注:如果 allure 找不到命令,就需要检查下环境变量是否正常。
1.安装:pip install pytest-html
2.运行:pytest --html=report/report.html --self-contained-html
--html:指定报告目录名称
--self-contained-html:可以直接打开显示(不会像allure显示Loading...)
对比 unittest 不需要封装一个runner all cases
G:\demo\pytest_demo\api_package>pytest cases --html=report/report.html --self-contained-html ================================================= test session starts ================================================= platform win32 -- Python 3.6.5, pytest-5.4.2, py-1.8.0, pluggy-0.13.1 rootdir: G:\demo\pytest_demo\api_package plugins: allure-pytest-2.8.13, html-2.1.1, metadata-1.9.0 collected 7 items cases\test_home.py ... [ 42%] cases\test_login.py .... [100%] ------------------- generated html file: file://G:\demo\pytest_demo\api_package\report\report.html -------------------- ================================================== 7 passed in 1.74s ==================================================
欢迎来大家QQ交流群一起学习:482713805
原文:https://www.cnblogs.com/gsxl/p/12853064.html