轉:https://www.cnblogs.com/yoyoketang/p/11575646.html
前言
前面幾篇零散的學了一些httprunner的知識點,那么一個完整的 YAML/JSON 用例文件包含哪些關鍵字呢?
測試用例結構
在 HttpRunner 中,測試用例組織主要基於三個概念:
- 測試用例集(testsuite):對應一個文件夾,包含單個或多個測試用例(YAML/JSON)文件
- 測試用例(testcase):對應一個 YAML/JSON 文件,包含單個或多個測試步驟
- 測試步驟(teststep):對應 YAML/JSON 文件中的一個 test,描述單次接口測試的全部內容,包括發起接口請求、解析響應結果、校驗結果等
對於單個 YAML/JSON 文件來說,數據存儲結構為 list of dict 的形式,其中可能包含一個全局配置項(config)和若干個測試步驟(test)。
- config:作為整個測試用例的全局配置項
- test:對應單個測試步驟(teststep),測試用例存在順序關系,運行時將從前往后依次運行各個測試步驟
對應的 JSON 格式如下所示:
[ { "config": {...} }, { "test": {...} }, { "test": {...} } ] 對應的 YAML 格式如下所示 - config: name: xxx - test: name: login case1 request: url: http://127.0.0.1:8000/api/v1/login/ ... - test: name: login case2 request: url: http://127.0.0.1:8000/api/v1/login/
變量空間(context)作用域
在測試用例內部,HttpRunner 划分了兩層變量空間作用域(context)。
- config:作為整個測試用例的全局配置項,作用域為整個測試用例;
- test:測試步驟的變量空間(context)會繼承或覆蓋 config 中定義的內容;
- 若某變量在 config 中定義了,在某 test 中沒有定義,則該 test 會繼承該變量
- 若某變量在 config 和某 test 中都定義了,則該 test 中使用自己定義的變量值
- 各個測試步驟(test)的變量空間相互獨立,互不影響;
- 如需在多個測試步驟(test)中傳遞參數值,則需要使用 extract 關鍵字,並且只能從前往后傳遞
config配置
| 關鍵字 | 是否必須 | 格式類型 | 描述 |
|---|---|---|---|
| name | Yes | string | 測試用例的名稱,在測試報告中將作為標題 |
| variables | No | list of dict | 定義的全局變量,作用域為整個用例 |
| parameters | No | list of dict | 全局參數,用於實現數據化驅動,作用域為整個用例 |
| request | No | dict | request 的公共參數,作用域為整個用例;常用參數包括 base_url 和 headers |
request相關參數
| 關鍵字 | 是否必須 | 格式類型 | 描述 |
|---|---|---|---|
| base_url | No | string | 測試用例請求 URL 的公共 host,指定該參數后,test 中的 url 可以只描述 path 部分 |
| headers | No | dict | request 中 headers 的公共參數,作用域為整個用例 |
| output | No | list | 整個用例輸出的參數列表,可輸出的參數包括公共的 variable 和 extract 的參數; 在 log-level 為 debug 模式下,會在 terminal 中打印出參數內容 |
config配置 JSON 格式示例
"config": { "name": "testcase description", "parameters": [ {"user_agent": ["iOS/10.1", "iOS/10.2", "iOS/10.3"]}, {"app_version": "${P(app_version.csv)}"}, {"os_platform": "${get_os_platform()}"} ], "variables": [ {"user_agent": "iOS/10.3"}, {"device_sn": "${gen_random_string(15)}"}, {"os_platform": "ios"} ], "request": { "base_url": "http://127.0.0.1:5000", "headers": { "Content-Type": "application/json", "device_sn": "$device_sn" } }, "output": [ "token" ] }
config配置 YAML 格式示例
- config: name: xxx parameters: - user_agent: ["iOS/10.1", "iOS/10.2", "iOS/10.3"] - app_version: ${P(app_version.csv)} - os_platform: ${get_os_platform()} variables: - user_agent: iOS/10.3 - device_sn: ${gen_random_string(15)} - os_platform: ios request: base_url: http://127.0.0.1:5000 headers: Content-Type: application/json device_sn: $device_sn output: - token
test相關參數
| 關鍵字 | 是否必須 | 格式類型 | 描述 |
|---|---|---|---|
| namel | Yes | string | 測試步驟的名稱,在測試報告中將作為測試步驟的名稱 |
| request | Yes | dict | HTTP 請求的詳細內容;可用參數詳見 python-requests 官方文檔 |
| variables | No | list of dict | 測試步驟中定義的變量,作用域為當前測試步驟 |
| extract | No | list | 從當前 HTTP 請求的響應結果中提取參數,並保存到參數變量中(例如token),后續測試用例可通過$token的形式進行引用 |
| validate | No | list | 測試用例中定義的結果校驗項,作用域為當前測試用例,用於實現對當前測試用例運行結果的校驗 |
| setup_hooks | No | list | 在 HTTP 請求發送前執行 hook 函數,主要用於准備工作 |
| teardown_hooks | No | list | 在 HTTP 請求發送后執行 hook 函數,主要用戶測試后的清理工作 |
test用例 JSON 格式示例
"test": { "name": "get token with $user_agent, $os_platform, $app_version", "request": { "url": "/api/get-token", "method": "POST", "headers": { "app_version": "$app_version", "os_platform": "$os_platform", "user_agent": "$user_agent" }, "json": { "sign": "${get_sign($user_agent, $device_sn, $os_platform, $app_version)}" }, "extract": [ {"token": "content.token"} ], "validate": [ {"eq": ["status_code", 200]}, {"eq": ["headers.Content-Type", "application/json"]}, {"eq": ["content.success", true]} ], "setup_hooks": [], "teardown_hooks": [] } }
test用例 YAML 格式示例
- test: name: get token with $user_agent, $os_platform, $app_version request: url: /api/get-token method: POST headers: app_version: $app_version os_platform: $os_platform user_agent: $user_agent json: sign: ${get_sign($user_agent, $device_sn, $os_platform, $app_version)} extract: - token: content.token validate: - eq: [status_code, 200] - eq: [headers.Content-Type, application/json] - eq: [content.success, true] setup_hooks: [] teardown_hooks: []
hooks
setup_hooks 函數放置於 debugtalk.py 中,並且必須包含三個參數:
- method: 請求方法,e.g. GET, POST, PUT
- url: 請求 URL
- kwargs: request 的參數字典
teardown_hooks 函數放置於 debugtalk.py 中,並且必須包含一個參數:
- resp_obj: requests.Response 實例
