Httprunner生成Allure格式HTML报告

目录

1. 前言

2. httprunner v2.x版本的报告

2.1 使用ExtentReport模板生成报告

2.2 指定ExtentReport为默认模板

3. httprunner v3.x版本的报告

3.1 模拟HTML 测试报告

3.2 allure报告


1. 前言

使用httprunner做接口自动化有一段时间了,发现httprunner相比pytest和unittest确实自己的优点。

  • 脚本能力要求较低
  • 完备且轻量的脚手架
  • 脚本产出速度快
  • 自带可视化的HTML报告

本篇文章不讨论如何使用httprunner,只讨论在使用httprunner后生成报告的几个方法,

2. httprunner v2.x版本的报告

在HttpRunner中,给我们提供了 2 套测试报告模板,分别是 default_report_template.html 和 extent_report_template.html 。

默认报告使用的是 default_report_template ,如果觉得不太美观,这时我们可以使用 extent_report_template ,其生成的报告相对更加漂亮,看起来也更加高大上。

2.1 使用ExtentReport模板生成报告

在安装HttpRunner的时候,如果是使用 pip 来安装,那么报告模板的路径在 python 安装路径下的 \Lib\site-packages\httprunner\templates 目录下,如我本地环境的报告模板路径:

报告模板路径

第一个是默认情况使用的报告模板,其生成的报告如下:

default_report_template

接下来,我们将使用第二个模板 extent_report_template.html 来生成报告。

在HttpRunner中,如果要指定测试报告的模板,需要通过 --html-report-template 指定报告模板的路径,然后测试运行完成后,就会采用指定模板生成测试报告。

如我在这里使用 extent_report_template 来生成报告:

点我复制hrun test_login.yml --html-report-template D:\Python\installation\Lib\site-packages\httprunner\templates\extent_report_template.html

运行完成后,会在当前路径的 reports 目录下生成一份 HTML 格式的测试报告,打开报告可看到:

extent_report_template

现在,我们对比一下,是不是能感觉到 extent_report_template 更加美观,逼格更高呢?

2.2 指定ExtentReport为默认模板

在上面步骤中,我们发现,使用 extent_report_template 来生成报告,运行命令会比较长,如果我们想将 extent_report_template 作为默认的测试报告模板,那么我们就需对 HttpRunner 的源码进行简单修改了。

HttpRunner中,生成报告相关的源码在 \Lib\site-packages\httprunner\report.py 下,打开文件并找到需要修改的地方:

def render_html_report(summary, html_report_name=None, html_report_template=None):
    """ render html report with specified report name and template
        if html_report_name is not specified, use current datetime
        if html_report_template is not specified, use default report template
    """
    if not html_report_template:
        html_report_template = os.path.join(
            os.path.abspath(os.path.dirname(__file__)),
            "templates",
            "default_report_template.html"
        )
        logger.log_debug("No html report template specified, use default.")
    else:
        logger.log_info("render with html report template: {}".format(html_report_template))

    logger.log_info("Start to render Html report ...")
    logger.log_debug("render data: {}".format(summary))

从源码中看到,如果不指定测试报告模板,那么参数 html_report_template=None ,使用的是 default_report_template.html 这个模板,因此,我们只要将这里修改一下,应该就能够达到我们的目标。

    if not html_report_template:
        html_report_template = os.path.join(
            os.path.abspath(os.path.dirname(__file__)),
            "templates",
            "extent_report_template.html"
        )

OK,修改之后,再次执行命令,可以发现,生成测试报告使用的默认模板已修改。

3. httprunner v3.x版本的报告

3.1 模拟HTML 测试报告

HTTPrunner 安装之后自带 pytest-html插件,当你想生成 HTML测试报告时,可以添加命令参数--html

 hrun testcases/dlvopenapi/dlvopenapi_success_test.py --html=test.html

--html=test.html`中的test.html是测试报告的存放路径,没有带文件夹的时候会存放在命令运行的当前文件夹,此处是项目根目录

hrun 命令生成报告的时候,带上 --html 指定报告路径

>hrun testcases/login_userinfo_test.py --html=./reports/result.html

--html 参数生成的报告,css文件是单独分开的,不方便查看,可以加上--self-contained-html参数让css文件集成到html上

>hrun testcases/login_userinfo_test.py --html=./reports/result.html --self-contained-html

生成的报告如下

在这里插入图片描述

3.2 allure报告

pytest 支持大名鼎鼎的 allure 测试报告,HTTPrunner 集成了pytest,也天然支持allure。不过 HTTPrunner 默认并未安装 allure,你需要另外安装。

安装有两种方式:

  • 安装allurepytest 依赖库allure-pytest
  • 安装 HTTPrunnerallure 依赖库 httprunner[allure]

安装 allure-pytest:

pip3 install allure-pytest

安装allure(第一种)

Mac:

brew install allure 

Windows:

https://repo.maven.apache.org/maven2/io/qameta/allure/allure-commandline/2.12.0/

下载后解压,进入bin目录,使用allure.bat即可,可将该目录配置到环境变量

或者安装allure(第二种)

安装 httprunner[allure](推荐)

pip3 install "httprunner[allure]"

一旦allure-pytest 准备好,以下参数就可以与 hrun/pytest命令一起使用:

step1:生成Allure报告数据

hrun --alluredir allure-results --clean-alluredir

  • --alluredir=DIR: 生成 allure 报告的原始数据到指定目录
  • -clean-alluredir: 如果指定目录已存在则清理该文件夹
  • --allure-no-capture:不要将 pytest 捕获的日志记录(logging)、标准输出(stdout)、标准错误(stderr)附加到报告中

step2:生成Allure报告

allure generate allure-results -o allure-report

-o 指定生成报告的文件夹
-c 在生成报告之前先清理之前的报告目录

要使 Allure 侦听器能够在测试执行期间收集结果,只需添加--alluredir选项,并提供指向存储结果的文件夹路径。如:

hrun testcases/dlvopenapi/dlvopenapi_success_test.py --alluredir=/tmp/my_allure_results

/tmp/my_allure_results 只会存储收集的测试结果并非完成的报告,还需要通过命令生成。

生成allure报告,执行:
allure generate ./tmp/my_allure_results -o ./tmp/allure-report --clean,运行后会生成一个report的文件夹,就是allure的报告

要在测试完成后查看实际报告,您需要使用Allure命令行实用程序从结果中生成报告。

显示报告,执行:
allure open -h 127.0.0.1 -p 8083 ./tmp/allure-report,显示报告

allure serve /tmp/my_allure_results

此命令将在默认浏览器中显示你生成的报告。

Allure的测试报告纬度多样且详细,还支持依据所需做个性化的定义。

查看Allure官方文档,获取更多Allure的用法。

 同时你也可以在 Jenkins 中安装 allure 报告插件,将结果与Jenkins 集成。

 有兴趣可以关注我的微信公众号“自动化测试全栈”,微信号:QAlife,学习更多自动化测试技术。

也可加入我们的自动化测试技术交流群,QQ群号码:301079813

主要探讨loadrunner/JMeter测试、Selenium/RobotFramework/Appium自动化测试、接口自动化测试,测试工具等测试技术,让我们来这里分享经验、交流技术、结交朋友、拓展视野、一起奋斗!

参考文档:

HttpRunner3.X学习笔记(7)- 测试报告 - 简书

HttpRunner学习9--切换测试报告模板 - wintest - 博客园

Httprunner生成Allure格式HTML报告 - 艾里_Simple - 博客园

HttpRunner 实现 Jinja2 自定义报告模板 · TesterHome

  • 1
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

慕城南风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值