1 Python HtmlTestRunner

HtmlTestRunner自定义模版
使用jinja2语法,自定义html文件报告内容

unittest单元测试框架:https://docs.python.org/zh-cn/3/library/unittest.html
Jinja官方文档地址:https://jinja.palletsprojects.com/en/2.10.x/


1 html报告默认参数
title:          - This is the report title - by default this is "Unittests Results" but can be changed using the report_title kwarg
headers:        - This is a dict with 2 items:
  start_time:   - A datetime object representing when the test was run
  status:       - A dict of of the same form as the sub-dicts described below for summaries but for all tests combined
all_results:    - A dict - keys are the names of each test case and values are lists containing test result objects (see the source code or the template for what information these provide)
summaries:      - A dict - keys are the names of each test case and values are dicts containing:
  total:        - The total number of tests
  success:      - The number of passed tests
  failure:      - The number of failed tests
  error:        - The number of errored tests
  skip:         - The number of skipped tests
  duration:     - A string showing how long all these tests took to run in either seconds or milliseconds
2 报告生成时 : 携带自定义参数
template_args = {
    "user": getpass.getuser()
}
runner = HtmlTestRunner.HTMLTestRunner(template='path/to/template', template_args=template_args)
3 简单的html模版



    {{ title }}
    
    
    


    

{{ title }}

Start Time: {{ header_info.start_time.strftime("%Y-%m-%d %H:%M:%S") }}

Duration: {{ header_info.duration }}

Summary: {{ header_info.status }}

{% for test_case_name, tests_results in all_results.items() %} {% if tests_results %}
{% for test_case in tests_results %} {% if test_case.stdout or test_case.err or test_case.err %} {% endif %} {% endfor %}
{{ test_case_name }} Status
{{ test_case.test_description }} {% if test_case.outcome == test_case.SUCCESS %} Pass {% elif test_case.outcome == test_case.SKIP %} Skip {% elif test_case.outcome == test_case.FAILURE %} Fail {% else %} Error {% endif %} {% if test_case.stdout or test_case.err %}   {% endif %}
{% if test_case.stdout %}

{{ test_case.stdout }}

{% endif %} {% if test_case.err %}

{{ test_case.err[0].__name__ }}: {{ test_case.err[1] }}

{% endif %} {% if test_case.err %}

{{ test_case.test_exception_info }}

{% endif %}
{{ summaries[test_case_name] }}
{% endif %} {% endfor %}
4Vue+iview 自定义报告,报告截图如下

-- Github 地址 https://github.com/mingyuanHub/python-game-test

1 Python HtmlTestRunner_第1张图片
image.png

你可能感兴趣的:(1 Python HtmlTestRunner)