httprunner学习10-测试报告ExtentReport

  • 2019 年 10 月 8 日
  • 笔记

前言

httprunner默认生成的报告不怎么美观,里面还有第二套报告模板extent_report_template.html。

extent_report_template

使用 hrun -h 可以看到运行的时候可以添加的命令行参数

C:Usersdell>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:python36Libsite-packageshttprunnertemplatesextent_report_template.html

hrun test_demo.yml —html-report-template /path/templatesextent_report_template.html

D:softuntitled>hrun test_demo.yml --html-report-template E:python36Libsite-packageshttprunnertemplatesextent_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:python36Libsite-packageshttprunnertemplatesextent_report_template.html  INFO     Start to render Html report ...  INFO     Generated Html report: D:softuntitledreports1569369482.html    D:softuntitled>

查看extentreport报告

查看extentreport测试报告,默认黑色主题

也可以切换成白色主题

默认使用extentreport报告

如果你不想每次输入这么长的参数,我们可以修改源码。默认使用extent_report_template.html 找到site-packageshttprunnerreport.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 defaul