专注于 JetBrains IDEA 全家桶,永久激活,教程
持续更新 PyCharm,IDEA,WebStorm,PhpStorm,DataGrip,RubyMine,CLion,AppCode 永久激活教程

HttpRunner学习9--切换测试报告模板

前言

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

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

本人环境:HttpRunner V1.5.8

使用ExtentReport模板生成报告

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

101_1.png

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

101_2.png

接下来,我们将使用第二个模板 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 格式的测试报告,打开报告可看到:

101_3.png

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

指定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,修改之后,再次执行命令,可以发现,生成测试报告使用的默认模板已修改。

文章永久链接:https://tech.souyunku.com/32933

未经允许不得转载:搜云库技术团队 » HttpRunner学习9--切换测试报告模板

JetBrains 全家桶,激活、破解、教程

提供 JetBrains 全家桶激活码、注册码、破解补丁下载及详细激活教程,支持 IntelliJ IDEA、PyCharm、WebStorm 等工具的永久激活。无论是破解教程,还是最新激活码,均可免费获得,帮助开发者解决常见激活问题,确保轻松破解并快速使用 JetBrains 软件。获取免费的破解补丁和激活码,快速解决激活难题,全面覆盖 2024/2025 版本!

联系我们联系我们