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,修改之后,再次執行命令,可以發現,生成測試報告使用的默認模板已修改。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM