httprunner學習10-測試報告ExtentReport


前言

httprunner默認生成的報告不怎么美觀,里面還有第二套報告模板extent_report_template.html。

extent_report_template

使用 hrun -h 可以看到運行的時候可以添加的命令行參數

C:\Users\dell>hrun -h
usage: hrun [-h] [-V] [--no-html-report] [--html-report-name HTML_REPORT_NAME]
            [--html-report-template HTML_REPORT_TEMPLATE]
            [--log-level LOG_LEVEL] [--log-file LOG_FILE]
            [--dot-env-path DOT_ENV_PATH] [--failfast]
            [--startproject STARTPROJECT]
            [--validate [VALIDATE [VALIDATE ...]]]
            [--prettify [PRETTIFY [PRETTIFY ...]]]
            [testset_paths [testset_paths ...]]

One-stop solution for HTTP(S) testing.

positional arguments:
  testset_paths         testset file path

optional arguments:
  -h, --help            show this help message and exit
  -V, --version         show version
  --no-html-report      do not generate html report.
  --html-report-name HTML_REPORT_NAME
                        specify html report name, only effective when
                        generating html report.
  --html-report-template HTML_REPORT_TEMPLATE
                        specify html report template path.
  --log-level LOG_LEVEL
                        Specify logging level, default is INFO.
  --log-file LOG_FILE   Write logs to specified file path.
  --dot-env-path DOT_ENV_PATH
                        Specify .env file path, which is useful for keeping
                        production credentials.
  --failfast            Stop the test run on the first error or failure.
  --startproject STARTPROJECT
                        Specify new project name.
  --validate [VALIDATE [VALIDATE ...]]
                        Validate JSON testset format.
  --prettify [PRETTIFY [PRETTIFY ...]]
                        Prettify JSON testset format.

使用 --html-report-template參數可以替換自己的模板,后面指定模板的路徑:E:\python36\Lib\site-packages\httprunner\templates\extent_report_template.html

hrun test_demo.yml --html-report-template /path/templates/extent_report_template.html

D:\soft\untitled>hrun test_demo.yml --html-report-template E:\python36\Lib\site-packages\httprunner\templates\extent_report_template.html
test_demo case1
INFO     GET http://127.0.0.1:8000/api/test/demo
INFO     status_code: 200, response_time(ms): 39.2 ms, response_length: 255 bytes
INFO     start to extract from response object.
INFO     start to validate.
.

----------------------------------------------------------------------
Ran 1 test in 0.049s

OK
INFO     render with html report template: E:\python36\Lib\site-packages\httprunner\templates\extent_report_template.html
INFO     Start to render Html report ...
INFO     Generated Html report: D:\soft\untitled\reports\1569369482.html

D:\soft\untitled>

查看extentreport報告

查看extentreport測試報告,默認黑色主題

也可以切換成白色主題

默認使用extentreport報告

如果你不想每次輸入這么長的參數,我們可以修改源碼。默認使用extent_report_template.html
找到\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))

修改后代碼

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",
            "extent_report_template.html"
        )
        logger.log_debug("No html report template specified, use extent_report_template.")
    elif html_report_template == 'default':
        html_report_template = os.path.join(
            os.path.abspath(os.path.dirname(__file__)),
            "templates",
            "default_report_template.html"
        )
        logger.log_debug("render with html report template: default_report_template.")
    else:
        logger.log_info("render with html report template: {}".format(html_report_template))

這樣默認就會生成extentreport

hrun test_demo.yml

帶上default參數就會生成原始的報告

hrun test_demo.yml --html-report-template default


免責聲明!

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



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