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 目录下,如我本地环境的报告模板路径:

报告模板路径

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

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 更加美观,逼格更高呢?

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

猜你喜欢

转载自www.cnblogs.com/wintest/p/11921714.html