pytest-3-allure生成测试报告

前言

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

1、实时生成 allure 报告

1.首先须要去github上下载最新版进行安装:https://github.com/allure-framework/allure2/releaseshtml

 2.allure 生成报告前提需安装好 pytest 与 allure-pytest :pip install pytest allure-pytest linux

pip install pytest allure-pytest 

 3.--alluredir 指定生成的json文件存放目录,如:pytest --alluredir ../report/allure_rawgit

 4.实时生成报告命令:allure serve json文件目录,运行命令后自动弹出web浏览器查看报告(右下角能够切换网页语言),以下:github

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报告会没有数据?web

显然是 allure serve 生成json文件目录不对应,因此要检查下目录是否正确。json

2、生成 html 报告文件

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

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...session

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

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

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

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

3、简单的 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:\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