录制快速生成测试用例

  • Fiddler/Charles抓包生成.har文件
  • har2cases生成yaml格式脚本
  • har2case生成json格式脚本

har2case相关指令

-h查看帮助选项

image-20210808174226576

Charles抓包及导出Har文件

我们本地启动项目,通过postman发送请求,charles抓包

PostMan代理配置

  1. 打开postman设置

    image-20210809184648006

  2. 设置代理

    image-20210809184755669

  3. 发送请求

    image-20210810110805656

Charles抓包

  1. 抓取请求

    image-20210809185018332

  2. 选中请求,并选择[Export Session…]

    image-20210809185236301

  3. 保存文件,选择.har格式

    image-20210810151924493

  4. 查看本地保存的文件

    image-20210810190549545

har2case将har文件转用例

har2case转yaml格式脚本

-2y参数是设置转成.yml格式的脚本,如果不加这个参数,默认转成json格式

1
2
3
4
5
6
7
8
lvjing@lvjingdeMacBook-Pro spring-boot-api % ls
login_har.har
lvjing@lvjingdeMacBook-Pro spring-boot-api % har2case -2y login_har.har
INFO:root:Start to generate testcase.
INFO:root:dump testcase to YAML format.
INFO:root:Generate YAML testcase successfully: login_har.yml
lvjing@lvjingdeMacBook-Pro spring-boot-api % ls
login_har.har login_har.yml

打开生成的login_har.yml文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
config:
name: testcase description
variables: {}
teststeps:
- name: /v1/user/login
request:
headers:
Content-Type: application/json
Postman-Token: d9089e0e-7a3e-45b5-8b2b-01382f2e74cc
User-Agent: PostmanRuntime/7.28.1
method: GET
url: http://127.0.0.1:8988/v1/user/login
validate:
- eq:
- status_code
- 200
- eq:
- headers.Content-Type
- application/json;charset=UTF-8
- eq:
- content.code
- '00000'
- eq:
- content.message
- 成功
- eq:
- content.success
- true

运行hrun login_har.yml文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
lvjing@lvjingdeMacBook-Pro spring-boot-api % hrun login_har.yml
INFO HttpRunner version: 2.5.7
INFO Start to run testcase: testcase description
/v1/user/login
INFO GET http://127.0.0.1:8988/v1/user/login
E

======================================================================
ERROR: test_0000_000 (httprunner.api.TestSequense)
/v1/user/login
----------------------------------------------------------------------
Traceback (most recent call last):
ConnectionRefusedError: [Errno 61] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x10a794220>: Failed to establish a new connection: [Errno 61] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='127.0.0.1', port=8988): Max retries exceeded with url: /v1/user/login (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x10a794220>: Failed to establish a new connection: [Errno 61] Connection refused'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
requests.exceptions.ConnectionError: HTTPConnectionPool(host='127.0.0.1', port=8988): Max retries exceeded with url: /v1/user/login (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x10a794220>: Failed to establish a new connection: [Errno 61] Connection refused'))

----------------------------------------------------------------------
Ran 1 test in 0.005s

FAILED (errors=1)
INFO Start to render Html report ...
INFO Generated Html report: /Users/lvjing/httprunner-project/spring-boot-api/reports/20210810T111302.013486.html
Sentry is attempting to send 0 pending error messages
Waiting up to 2 seconds
Press Ctrl-C to quit

har2case转json格式脚本

不需要增加参数,可直接生成

1
2
3
4
5
6
lvjing@lvjingdeMacBook-Pro spring-boot-api % har2case login_har.har
INFO:root:Start to generate testcase.
INFO:root:dump testcase to JSON format.
INFO:root:Generate JSON testcase successfully: login_har.json
lvjing@lvjingdeMacBook-Pro spring-boot-api % ls
login_har.har login_har.json

打开生成的login_har.json文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
{
"config": {
"name": "testcase description",
"variables": {}
},
"teststeps": [
{
"name": "/v1/user/login",
"request": {
"url": "http://127.0.0.1:8988/v1/user/login",
"method": "GET",
"headers": {
"Content-Type": "application/json",
"User-Agent": "PostmanRuntime/7.28.1",
"Postman-Token": "d9089e0e-7a3e-45b5-8b2b-01382f2e74cc"
}
},
"validate": [
{
"eq": [
"status_code",
200
]
},
{
"eq": [
"headers.Content-Type",
"application/json;charset=UTF-8"
]
},
{
"eq": [
"content.code",
"00000"
]
},
{
"eq": [
"content.message",
"成功"
]
},
{
"eq": [
"content.success",
true
]
}
]
}
]
}

运行hrun login_har.json文件