美文网首页Python接口测试Python自动化测试
8 Python HtmlTestRunner自定义模版

8 Python HtmlTestRunner自定义模版

作者: 降水 | 来源:发表于2019-08-16 17:55 被阅读0次

    HtmlTestRunner自定义模版
    使用jinja2语法,自定义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模版
    <!DOCTYPE html>
    <html>
    <head>
        <title>{{ title }}</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
    </head>
    <body>
        <div class="container">
            <div class="row">
                <div class="col-xs-12">
                    <h2 class="text-capitalize">{{ title }}</h2>
                    <p class='attribute'><strong>Start Time: </strong>{{ header_info.start_time.strftime("%Y-%m-%d %H:%M:%S") }}</p>
                    <p class='attribute'><strong>Duration: </strong>{{ header_info.duration }}</p>
                    <p class='attribute'><strong>Summary: </strong>{{ header_info.status }}</p>
                </div>
            </div>
            {% for test_case_name, tests_results in all_results.items() %}
            {% if tests_results %}
            <div class="row">
                <div class="col-xs-12 col-sm-10 col-md-10">
                    <table class='table table-hover table-responsive'>
                        <thead>
                            <tr>
                                <th>{{ test_case_name }}</th>
                                <th>Status</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for test_case in tests_results %}
                            <tr class='{{ status_tags[test_case.outcome] }}'>
                                <td class="col-xs-9">{{ test_case.test_description }}</td>
                                <td class="col-xs-3">
                                    <span class="label label-{{ status_tags[test_case.outcome] }}">
                                        {% 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 %}
                                    </span>
                                    {% if test_case.stdout or test_case.err %}
                                        &nbsp<button class="btn btn-default btn-xs">View</button>
                                    {% endif %}
                                </td>
                            </tr>
                            {% if test_case.stdout or test_case.err or test_case.err %}
                                <tr style="display:none;">
                                    <td class="col-xs-9">
                                        {% if test_case.stdout %}<p>{{ test_case.stdout }}</p>{% endif %}
                                        {% if test_case.err %}<p style="color:maroon;">{{ test_case.err[0].__name__ }}: {{ test_case.err[1] }}</p>{% endif %}
                                        {% if test_case.err %}<p style="color:maroon;">{{ test_case.test_exception_info }}</p>{% endif %}
                                    </td>
                                </tr>
                            {% endif %}
                            {% endfor %}
                            <tr>
                                <td>
                                   {{ summaries[test_case_name] }}
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
            {% endif %}
            {% endfor %}
        </div>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function(){
                $('td').on('click', '.btn', function(e){
                    e.preventDefault();
                    var $this = $(this);
                    var $nextRow = $this.closest('tr').next('tr');
                    $nextRow.slideToggle("fast");
                    $this.text(function(i, text){
                        if (text === 'View') {
                            return 'Hide';
                        } else {
                            return 'View';
                        };
                    });
                });
            });
        </script>
    </body>
    </html>
    
    4Vue+iview 自定义报告,报告截图如下

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

    image.png

    相关文章

      网友评论

        本文标题:8 Python HtmlTestRunner自定义模版

        本文链接:https://www.haomeiwen.com/subject/afiqsctx.html