在編寫測試用例之前,我們應該瞭解 API 的詳細信息。使用 Web 調試代理工具(如Charles,Fiddler)捕獲 HTTP 是一個不錯的選擇。
httprunner 提供一個命令行工具har2case
,用於將抓包工具導出的 har 文件轉化爲 httprunner 格式的用例,並加上了一些簡單的斷言。
通過這種方式可以實現錄製界面操作並生成測試用例。
錄製
這是一個簡單的示例,你可以訪問 postman-echo.com 中的示例請求來嘗試這個案例。
1. 抓包
2. 導出 har
選擇捕獲的請求和響應,並 HTTP請求信息.har
文件。
3. Fiddler 保存 har
4. 通過har2case
命令生成用例
獲取.har
文件後,可以使用內置命令har2case
將其轉換爲 HttpRunner 測試用例。
har2case 命令幫助:
$ har2case -h
usage: har2case har2case [-h] [-2y] [-2j] [--filter FILTER]
[--exclude EXCLUDE]
[har_source_file]
positional arguments:
har_source_file 指定 .har 源文件
optional arguments:
-h, --help 顯示此幫助信息並退出
-2y, --to-yml, --to-yaml
轉換爲 YAML 格式的用例,
如果你沒有特殊指定,默認轉化爲 pytest 格式的用例
-2j, --to-json 轉換爲 JSON 格式的用例,
如果你沒有特殊指定,默認轉化爲 pytest 格式的用例
--filter FILTER 指定過濾關鍵字,只有包含過濾字符串的 url 的 API 纔會被轉換
--exclude EXCLUDE 指定忽略關鍵字,如果 url 包含該關鍵字,則會被忽略,
如果需要過濾多個關鍵字,使用`|`(豎線)分隔
生成用例
1. pytest 格式用例
從 HttpRunner 3.0.7 開始,默認情況下har2case
會將 HAR 文件轉換爲 pytest 格式的用例。
強烈建議以
pytest
格式而不是以前的YAML/JSON
格式編寫和維護測試用例。
$ har2case har/postman-echo-post-form.har
2020-06-15 15:08:01.187 | INFO | httprunner.ext.har2case.core:gen_testcase:332 - Start to generate testcase from har/postman-echo-post-form.har
2020-06-15 15:08:01.187 | INFO | httprunner.ext.har2case.core:_make_testcase:323 - Extract info from HAR file and prepare for testcase.
2020-06-15 15:08:01.191 | INFO | httprunner.loader:load_dot_env_file:130 - Loading environment variables from /Users/debugtalk/Desktop/demo/.env
2020-06-15 15:08:01.191 | DEBUG | httprunner.utils:set_os_environ:32 - Set OS environment variable: USERNAME
2020-06-15 15:08:01.191 | DEBUG | httprunner.utils:set_os_environ:32 - Set OS environment variable: PASSWORD
2020-06-15 15:08:01.193 | INFO | httprunner.make:make_testcase:310 - start to make testcase: /Users/debugtalk/Desktop/demo/har/postman-echo-post-form.har
2020-06-15 15:08:01.193 | INFO | httprunner.make:make_testcase:383 - generated testcase: /Users/debugtalk/Desktop/demo/har/postman_echo_post_form_test.py
2020-06-15 15:08:01.194 | INFO | httprunner.make:format_pytest_with_black:147 - format pytest cases with black ...
reformatted /Users/debugtalk/Desktop/demo/har/postman_echo_post_form_test.py
All done! ✨ 🍰 ✨
1 file reformatted.
2020-06-15 15:08:01.469 | INFO | httprunner.ext.har2case.core:gen_testcase:353 - generated testcase: /Users/debugtalk/Desktop/demo/har/postman_echo_post_form_test.py
生成的 pytest 文件爲如下所示的標準 Python 文件:
# NOTE: Generated By HttpRunner v3.0.12
# FROM: har/postman-echo-post-form.har
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
class TestCasePostmanEchoPostForm(HttpRunner):
config = Config("testcase description").verify(False)
teststeps = [
Step(
RunRequest("/get")
.get("https://postman-echo.com/get")
.with_params(**{"foo1": "bar1", "foo2": "bar2"})
.with_headers(
**{
"User-Agent": "PostmanRuntime/7.24.1",
"Accept": "*/*",
"Cache-Control": "no-cache",
"Postman-Token": "6606343b-10e5-4165-a89f-6c301b762ce0",
"Host": "postman-echo.com",
"Accept-Encoding": "gzip, deflate, br",
"Connection": "keep-alive",
"Cookie": "sails.sid=s%3AQG_EVeNRw8k1xxZ6v_SG401VTpmJDSRu.fTAGx3JnZUT7S0c2%2FrD9cxUhQemIsm78nifYZYHpPCU",
}
)
.with_cookies(
**{
"sails.sid": "s%3AQG_EVeNRw8k1xxZ6v_SG401VTpmJDSRu.fTAGx3JnZUT7S0c2%2FrD9cxUhQemIsm78nifYZYHpPCU"
}
)
.validate()
.assert_equal("status_code", 200)
.assert_equal('headers."Content-Type"', "application/json; charset=utf-8")
.assert_equal(
"body.url", "https://postman-echo.com/get?foo1=bar1&foo2=bar2"
)
),
Step(
RunRequest("/post")
.post("https://postman-echo.com/post")
.with_headers(
**{
"User-Agent": "PostmanRuntime/7.24.1",
"Accept": "*/*",
"Cache-Control": "no-cache",
"Postman-Token": "3e408e9d-25ca-4b31-b04b-7f4898a8cd49",
"Host": "postman-echo.com",
"Accept-Encoding": "gzip, deflate, br",
"Connection": "keep-alive",
"Content-Type": "application/x-www-form-urlencoded",
"Content-Length": "19",
"Cookie": "sails.sid=s%3AQG_EVeNRw8k1xxZ6v_SG401VTpmJDSRu.fTAGx3JnZUT7S0c2%2FrD9cxUhQemIsm78nifYZYHpPCU",
}
)
.with_cookies(
**{
"sails.sid": "s%3AQG_EVeNRw8k1xxZ6v_SG401VTpmJDSRu.fTAGx3JnZUT7S0c2%2FrD9cxUhQemIsm78nifYZYHpPCU"
}
)
.with_data({"foo1": "bar1", "foo2": "bar2"})
.validate()
.assert_equal("status_code", 200)
.assert_equal('headers."Content-Type"', "application/json; charset=utf-8")
.assert_equal("body.data", "")
.assert_equal("body.url", "https://postman-echo.com/post")
),
]
if __name__ == "__main__":
TestCasePostmanEchoPostForm().test_start()
然後可以使用hrun
命令或pytest
命令運行。實際上,hrun
只是pytest
的包裝,因此效果基本相同。
$ hrun har/postman_echo_post_form_test.py
2020-06-15 15:23:03.502 | INFO | httprunner.loader:load_dot_env_file:130 - Loading environment variables from /Users/debugtalk/Desktop/demo/.env
2020-06-15 15:23:03.502 | DEBUG | httprunner.utils:set_os_environ:32 - Set OS environment variable: USERNAME
2020-06-15 15:23:03.502 | DEBUG | httprunner.utils:set_os_environ:32 - Set OS environment variable: PASSWORD
2020-06-15 15:23:03.503 | INFO | httprunner.make:format_pytest_with_black:147 - format pytest cases with black ...
All done! ✨ 🍰 ✨
1 file left unchanged.
2020-06-15 15:23:03.662 | INFO | httprunner.cli:main_run:56 - start to run tests with pytest. HttpRunner version: 3.0.12
========================== test session starts ==========================
platform darwin -- Python 3.7.5, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: /Users/debugtalk/Desktop/demo
plugins: metadata-1.9.0, allure-pytest-2.8.16, html-2.1.1
collected 1 item
har/postman_echo_post_form_test.py . [100%]
=========================== 1 passed in 2.60s =============================
$ pytest har/postman_echo_post_form_test.py
=========================== test session starts ===========================
platform darwin -- Python 3.7.5, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: /Users/debugtalk/Desktop/demo
plugins: metadata-1.9.0, allure-pytest-2.8.16, html-2.1.1
collected 1 item
har/postman_echo_post_form_test.py . [100%]
============================= 1 passed, 1 warning in 4.11s =============================
2. YAML/JSON 格式用例
當然,也可以生成以前的 YAML/JSON 測試用例格式。只需在har2case
中添加-2y/-to-yml
或-2j/-to-json
參數即可。
$ har2case har/postman-echo-post-form.har -2j
2020-06-15 15:32:02.955 | INFO | httprunner.ext.har2case.core:gen_testcase:332 - Start to generate testcase from har/postman-echo-post-form.har
2020-06-15 15:32:02.955 | INFO | httprunner.ext.har2case.core:_make_testcase:323 - Extract info from HAR file and prepare for testcase.
2020-06-15 15:32:02.958 | INFO | httprunner.ext.har2case.utils:dump_json:122 - dump testcase to JSON format.
2020-06-15 15:32:02.959 | INFO | httprunner.ext.har2case.utils:dump_json:131 - Generate JSON testcase successfully: har/postman-echo-post-form.json
2020-06-15 15:32:02.959 | INFO | httprunner.ext.har2case.core:gen_testcase:353 - generated testcase: har/postman-echo-post-form.json
{
"config": {
"name": "testcase description",
"variables": {},
"verify": false
},
"teststeps": [
{
"name": "/get",
"request": {
"url": "https://postman-echo.com/get",
"params": {
"foo1": "bar1",
"foo2": "bar2"
},
"method": "GET",
"cookies": {
"sails.sid": "s%3AQG_EVeNRw8k1xxZ6v_SG401VTpmJDSRu.fTAGx3JnZUT7S0c2%2FrD9cxUhQemIsm78nifYZYHpPCU"
},
"headers": {
"User-Agent": "PostmanRuntime/7.24.1",
"Accept": "*/*",
"Cache-Control": "no-cache",
"Postman-Token": "6606343b-10e5-4165-a89f-6c301b762ce0",
"Host": "postman-echo.com",
"Accept-Encoding": "gzip, deflate, br",
"Connection": "keep-alive",
"Cookie": "sails.sid=s%3AQG_EVeNRw8k1xxZ6v_SG401VTpmJDSRu.fTAGx3JnZUT7S0c2%2FrD9cxUhQemIsm78nifYZYHpPCU"
}
},
"validate": [
{
"eq": [
"status_code",
200
]
},
{
"eq": [
"headers.Content-Type",
"application/json; charset=utf-8"
]
},
{
"eq": [
"body.url",
"https://postman-echo.com/get?foo1=bar1&foo2=bar2"
]
}
]
},
{
"name": "/post",
"request": {
"url": "https://postman-echo.com/post",
"method": "POST",
"cookies": {
"sails.sid": "s%3AQG_EVeNRw8k1xxZ6v_SG401VTpmJDSRu.fTAGx3JnZUT7S0c2%2FrD9cxUhQemIsm78nifYZYHpPCU"
},
"headers": {
"User-Agent": "PostmanRuntime/7.24.1",
"Accept": "*/*",
"Cache-Control": "no-cache",
"Postman-Token": "3e408e9d-25ca-4b31-b04b-7f4898a8cd49",
"Host": "postman-echo.com",
"Accept-Encoding": "gzip, deflate, br",
"Connection": "keep-alive",
"Content-Type": "application/x-www-form-urlencoded",
"Content-Length": "19",
"Cookie": "sails.sid=s%3AQG_EVeNRw8k1xxZ6v_SG401VTpmJDSRu.fTAGx3JnZUT7S0c2%2FrD9cxUhQemIsm78nifYZYHpPCU"
},
"data": {
"foo1": "bar1",
"foo2": "bar2"
}
},
"validate": [
{
"eq": [
"status_code",
200
]
},
{
"eq": [
"headers.Content-Type",
"application/json; charset=utf-8"
]
},
{
"eq": [
"body.data",
""
]
},
{
"eq": [
"body.url",
"https://postman-echo.com/post"
]
}
]
}
]
}
YAML/JSON 測試用例與 pytest 測試用例具有相同的信息,並且可以使用hrun
命令運行 YAML/JSON 測試用例。
$ hrun har/postman-echo-post-form.json
2020-06-15 15:37:15.621 | INFO | httprunner.loader:load_dot_env_file:130 - Loading environment variables from /Users/debugtalk/Desktop/demo/.env
2020-06-15 15:37:15.622 | DEBUG | httprunner.utils:set_os_environ:32 - Set OS environment variable: USERNAME
2020-06-15 15:37:15.622 | DEBUG | httprunner.utils:set_os_environ:32 - Set OS environment variable: PASSWORD
2020-06-15 15:37:15.623 | INFO | httprunner.make:make_testcase:310 - start to make testcase: /Users/debugtalk/Desktop/demo/har/postman-echo-post-form.json
2020-06-15 15:37:15.625 | INFO | httprunner.make:make_testcase:383 - generated testcase: /Users/debugtalk/Desktop/demo/har/postman_echo_post_form_test.py
2020-06-15 15:37:15.625 | INFO | httprunner.make:format_pytest_with_black:147 - format pytest cases with black ...
reformatted /Users/debugtalk/Desktop/demo/har/postman_echo_post_form_test.py
All done! ✨ 🍰 ✨
1 file reformatted, 1 file left unchanged.
2020-06-15 15:37:15.962 | INFO | httprunner.cli:main_run:56 - start to run tests with pytest. HttpRunner version: 3.0.12
================================= test session starts ===============================
platform darwin -- Python 3.7.5, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: /Users/debugtalk/Desktop/demo
plugins: metadata-1.9.0, allure-pytest-2.8.16, html-2.1.1
collected 1 item
har/postman_echo_post_form_test.py . [100%]
=============================== 1 passed in 2.03s ===================================