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實戰教程點我 ->立即報名


免責聲明!

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



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