httprunner 2.x学习17 - summary结果统计和 html 报告生成

前言

调用HttpRunner类的run方法运行用例后,会返回测试结果统计。
得到测试结果后加载html测试报告模板,最终得到一个html测试报告。

summary结果统计

接着上一篇调用HttpRunner类执行用例https://www.cnblogs.com/yoyoketang/p/15210874.html

from httprunner.api import HttpRunner
import json
# 作者-上海悠悠 QQ交流群:717225969
# blog地址 https://www.cnblogs.com/yoyoketang/

runner = HttpRunner(log_level="INFO")
summary = runner.run('login/login1.yml')
print(summary)

# 美化输出结果
print(json.dumps(summary, indent=4))

summary结构如下

{
    "success": true,
    "stat": {
        "testcases": {
            "total": 1,
            "success": 1,
            "fail": 0
        },
        "teststeps": {
            "total": 1,
            "failures": 0,
            "errors": 0,
            "skipped": 0,
            "expectedFailures": 0,
            "unexpectedSuccesses": 0,
            "successes": 1
        }
    },
    "time": {
        "start_at": 1630412192.7478883,
        "duration": 0.32332420349121094
    },
    "platform": {
        "httprunner_version": "2.5.7",
        "python_version": "CPython 3.6.6",
        "platform": "Windows-10-10.0.17134-SP0"
    },
    "details": [
        {
            "success": true,
            "stat": {
                "total": 1,
                "failures": 0,
                "errors": 0,
                "skipped": 0,
                "expectedFailures": 0,
                "unexpectedSuccesses": 0,
                "successes": 1
            },
            "time": {
                "start_at": 1630412192.7478883,
                "duration": 0.32332420349121094
            },
            "records": [
                {
                    "name": "login case1",
                    "status": "success",
                    "attachment": "",
                    "meta_datas": {
                        "name": "login case1",
                        "data": [
                            {
                                "request": {
                                    "url": "http://127.0.0.1:8000/api/v1/login",
                                    "method": "POST",
                                    "headers": "{\n  \"User-Agent\": \"python-requests/2.18.4\",\n  \"Accept-Encoding\": \"gzip}",
                                    "body": "{\n  "username": "test",\n  "password": 123456\n}"
                                },
                                "response": {
                                    "ok": true,
                                    "url": "http://127.0.0.1:8000/api/v1/login",
                                    "status_code": 200,
                                    "reason": "OK",
                                    "cookies": "{}",
                                    "encoding": "utf-8",
                                    "headers": "{\n  \"Date\": \"Tue, 31 Aug 2021 12:16:34 GMT\",\n  \"Content-Type\": \"application/json\",}",
                                    "content_type": "application/json",
                                    "body": "{\n  \"code\": 0,\n  \"msg\": \"login success!\",\n  \"username\": \"test\",\n  \"token\": \"8574e73da21a954633703719c7171020326e5478\"\n}"
                                }
                            }
                        ],
                        "stat": {
                            "response_time_ms": 318.78,
                            "elapsed_ms": 315.666,
                            "content_size": 109
                        },
                        "validators": {
                            "validate_extractor": [
                                {
                                    "comparator": "equals",
                                    "check": "status_code",
                                    "check_value": 200,
                                    "expect": 200,
                                    "expect_value": 200,
                                    "check_result": "pass"
                                }
                            ]
                        }
                    },
                    "meta_datas_expanded": [
                        {
                            "name": "login case1",
                            "data": [
                                {
                                    "request": {
                                        "url": "http://127.0.0.1:8000/api/v1/login",
                                        "method": "POST",
                                        "headers": "{\n  \"User-Agent\": \"python-requests/2.18.4\",\n  \"Accept-Encoding\": \"gzip, deflate\"}",
                                        "body": "{\n  "username": "test",\n  "password": 123456\n}"
                                    },
                                    "response": {
                                        "ok": true,
                                        "url": "http://127.0.0.1:8000/api/v1/login",
                                        "status_code": 200,
                                        "reason": "OK",
                                        "cookies": "{}",
                                        "encoding": "utf-8",
                                        "headers": "{\n  \"Date\": \"Tue, 31 Aug 2021 12:16:34 GMT\",\n \"Content-Type\": \"application/json\"}",
                                        "content_type": "application/json",
                                        "body": "{\n  \"code\": 0,\n  \"msg\": \"login success!\",\n  \"username\": \"test\",\n  \"token\": \"8574e73da21a954633703719c7171020326e5478\"\n}"
                                    }
                                }
                            ],
                            "stat": {
                                "response_time_ms": 318.78,
                                "elapsed_ms": 315.666,
                                "content_size": 109
                            },
                            "validators": {
                                "validate_extractor": [
                                    {
                                        "comparator": "equals",
                                        "check": "status_code",
                                        "check_value": 200,
                                        "expect": 200,
                                        "expect_value": 200,
                                        "check_result": "pass"
                                    }
                                ]
                            }
                        }
                    ],
                    "response_time": "318.78"
                }
            ],
            "name": "logincase",
            "in_out": {
                "in": {},
                "out": {}
            }
        }
    ]
}

生成 HTML 测试报告

生成html报告调用get_report.py 里面的 gen_html_report()函数

def gen_html_report(summary, report_template=None, report_dir=None, report_file=None):
    """ render html report with specified report name and template

    Args:
        summary (dict): test result summary data
        report_template (str): specify html report template path, template should be in Jinja2 format.
        report_dir (str): specify html report save directory
        report_file (str): specify html report file path, this has higher priority than specifying report dir.

    """

gen_html_report()函数可以传四个参数

  • summary 上一步运行用例后生成的汇总报告(dict)
  • report_template 指定自定义的 HTML 报告模板,模板必须采用 Jinja2 的格式
  • report_dir(可选): 指定生成报告的文件夹路径
  • report_file(可选): 指定生成报告的文件路径,该参数的优先级高于 report_dir

report_template 模板地址默认为None,默认生成自带的模板报告

from httprunner.api import HttpRunner
from httprunner.report import gen_html_report
import json
# 作者-上海悠悠 QQ交流群:717225969
# blog地址 https://www.cnblogs.com/yoyoketang/


runner = HttpRunner(log_level="INFO")
summary = runner.run('login/login1.yml')
print(summary)

# # 美化输出结果
# print(json.dumps(summary, indent=4))

# 生成测试报告
gen_html_report(summary=summary)

运行后默认在项目根目录生成reports文件夹

html报告打开

可以点开详情查看

report_template参数可以自定义模板,参考https://www.cnblogs.com/yoyoketang/p/13121392.html


httprunner 2.x实战教程点我 ->立即报名

posted @ 2021-08-31 20:32  上海-悠悠  阅读(749)  评论(0编辑  收藏  举报