pytest-3-allure生成测试报告

前言

广深小龙最近有写 pytest + request + yaml + allure...的测试框架,认为总体还不错。pytest 单元测试框架需配合 allure 生成漂亮的测试报告。

一、实时生成 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:demopytest_demoapi_packagecases>allure serve ../report/allure_raw
Generating report to temp directory...
Report successfully generated to C:UsersADMINI~1AppDataLocalTemp7463144076250537920allure-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文件目录不对应,所以要检查下目录是否正确。

二、生成 html 报告文件

1.命令:allure generate json文件目录 html生成目录   (html目录没有会自动生成)

G:demopytest_demoapi_packagecases>allure generate ../report/allure_raw -o ../report/html/
Report successfully generated to ..
eporthtml

2.生成html后,右键 index.html 文件打开 Open in Browser 选择一个浏览器打开。

3.你会发现我直接在桌面用一般浏览器包括 Chrom 打开显示没有数据,显示Loading...

这是因为 html 导入css、js是相对路径浏览器识别不出来。

最后解决思路(linux上持续集成,推荐第3):

  • 1.使用:firefox 浏览器直接打开(此浏览器能识别整个html文件)
  • 2.Pycharm 中 Open in Browser
  • 3.部署一个小web项目,访问一个链接就能看到

注:如果 allure 找不到命令,就需要检查下环境变量是否正常。

三、简单的 pytest html报告

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:demopytest_demoapi_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:demopytest_demoapi_package
plugins: allure-pytest-2.8.13, html-2.1.1, metadata-1.9.0
collected 7 items

cases	est_home.py ...                                                                                           [ 42%]
cases	est_login.py ....                                                                                         [100%]

------------------- generated html file: file://G:demopytest_demoapi_package
eport
eport.html --------------------
================================================== 7 passed in 1.74s ==================================================

欢迎来大家QQ交流群一起学习:482713805

原文地址:https://www.cnblogs.com/gsxl/p/12853064.html