抓包生成测试用例
charles抓包
表单数据发布到xueyu_scrapy.online
会话导出har文件
选择捕获的请求和响应,会话导出har文件
har2case生成测试用例
内置命令har2case,转换为httprunner测试用例
har2case帮助
har2case -h
usage: har2case har2case [-h] [-2y] [-2j] [--filter FILTER]
[--exclude EXCLUDE]
[har_source_file]
positional arguments:
har_source_file Specify HAR source file
optional arguments:
-h, --help show this help message and exit
-2y, --to-yml, --to-yaml
Convert to YAML format, if not specified, convert to
pytest format by default.
-2j, --to-json Convert to JSON format, if not specified, convert to
pytest format by default.
--filter FILTER Specify filter keyword, only url include filter string
will be converted.
--exclude EXCLUDE Specify exclude keyword, url that includes exclude
string will be ignored, multiple keywords can be
joined with '|'
生成测试用例(pytest)
httprunner 3.0.7,har2case默认会将 HAR 文件转换为 pytest,不推荐yaml/json格式
# 生成测试用例(pytest)
har2case har/xueyu_scrapy.har
2020-06-15 15:08:01.187 | INFO | httprunner.ext.har2case.core:gen_testcase:332 - Start to generate testcase from har/xueyu_scrapy.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/xueyu_scrapy.har
2020-06-15 15:08:01.193 | INFO | httprunner.make:make_testcase:383 - generated testcase: /Users/debugtalk/Desktop/demo/har/xueyu_scrapy.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/xueyu_scrapy.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/xueyu_scrapy.py
# 生成的测试用例(pytest)
# NOTE: Generated By httprunner v3.0.12
# FROM: har/xueyu_scrapy.har
from httprunner import httprunner, Config, Step, RunRequest, RunTestCase
class TestCasexueyu_scrapyEchoPostForm(httprunner):
config = Config("testcase description").verify(False)
teststeps = [
Step(
RunRequest("/get")
.get("http://xueyu_scrapy.online/get")
.with_params(**{"foo1": "bar1", "foo2": "bar2"})
.with_headers(
**{
"User-Agent": "xueyu_scrapyRuntime/7.24.1",
"Accept": "*/*",
"Cache-Control": "no-cache",
"xueyu_scrapy-Token": "6606343b-10e5-4165-a89f-6c301b762ce0",
"Host": "xueyu_scrapy.online",
"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", "http://xueyu_scrapy.online/get?foo1=bar1&foo2=bar2"
)
),
Step(
RunRequest("/post")
.post("http://xueyu_scrapy.online/post")
.with_headers(
**{
"User-Agent": "xueyu_scrapyRuntime/7.24.1",
"Accept": "*/*",
"Cache-Control": "no-cache",
"xueyu_scrapy-Token": "3e408e9d-25ca-4b31-b04b-7f4898a8cd49",
"Host": "xueyu_scrapy.online",
"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", "http://xueyu_scrapy.online/post")
),
]
if __name__ == "__main__":
TestCasexueyu_scrapyEchoPostForm().test_start()
hrun命令或pytest命令运行
hrun包装pytest了,效果基本相同
hrun har/xueyu_scrapy.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/xueyu_scrapy.py . [100%]
======================================================================= 1 passed in 2.60s =======================================================================
pytest har/xueyu_scrapy.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/xueyu_scrapy.py . [100%]
================================================================= 1 passed, 1 warning in 4.11s ==================================================================
生成测试用例 (yaml/json)
生成yaml/json测试用例,只需添加-2y/--to-yml或-2j/--to-json参数到har2case
# 生成yml测试用例 (yaml/json)
har2case har/xueyu_scrapy.har -2j
2020-06-15 15:32:02.955 | INFO | httprunner.ext.har2case.core:gen_testcase:332 - Start to generate testcase from har/xueyu_scrapy.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/xueyu_scrapy.json
2020-06-15 15:32:02.959 | INFO | httprunner.ext.har2case.core:gen_testcase:353 - generated testcase: har/xueyu_scrapy.json
{
"config": {
"name": "testcase description",
"variables": {},
"verify": false
},
"teststeps": [
{
"name": "/get",
"request": {
"url": "http://xueyu_scrapy.online/get",
"params": {
"foo1": "bar1",
"foo2": "bar2"
},
"method": "GET",
"cookies": {
"sails.sid": "s%3AQG_EVeNRw8k1xxZ6v_SG401VTpmJDSRu.fTAGx3JnZUT7S0c2%2FrD9cxUhQemIsm78nifYZYHpPCU"
},
"headers": {
"User-Agent": "xueyu_scrapyRuntime/7.24.1",
"Accept": "*/*",
"Cache-Control": "no-cache",
"xueyu_scrapy-Token": "6606343b-10e5-4165-a89f-6c301b762ce0",
"Host": "xueyu_scrapy.online",
"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",
"http://xueyu_scrapy.online/get?foo1=bar1&foo2=bar2"
]
}
]
},
{
"name": "/post",
"request": {
"url": "http://xueyu_scrapy.online/post",
"method": "POST",
"cookies": {
"sails.sid": "s%3AQG_EVeNRw8k1xxZ6v_SG401VTpmJDSRu.fTAGx3JnZUT7S0c2%2FrD9cxUhQemIsm78nifYZYHpPCU"
},
"headers": {
"User-Agent": "xueyu_scrapyRuntime/7.24.1",
"Accept": "*/*",
"Cache-Control": "no-cache",
"xueyu_scrapy-Token": "3e408e9d-25ca-4b31-b04b-7f4898a8cd49",
"Host": "xueyu_scrapy.online",
"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",
"http://xueyu_scrapy.online/post"
]
}
]
}
]
}
# 生成的测试用例 (yaml/json)
hrun har/xueyu_scrapy.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/xueyu_scrapy.json
2020-06-15 15:37:15.625 | INFO | httprunner.make:make_testcase:383 - generated testcase: /Users/debugtalk/Desktop/demo/har/xueyu_scrapy.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/xueyu_scrapy.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/xueyu_scrapy.py . [100%]
======================================================================= 1 passed in 2.03s =======================================================================