测试框架学习之HttpRunner测试用例准备(三)

测试用例准备(有以下一些方式录制进行转换,以Charles为例)


1.1、Charles Proxy工具抓包并导出.har数据包

 

 

1.2、fiddler工具抓包并导出.har数据包

 

 

 



2、命令行终端中运行 har2case 命令,即可将 demo.har 转换为 HttpRunner 的测试用例文件(YAML 或 JSON 格式,第二个参数为空,则默认转换为 JSON 格式)
2.1、har2case 可将 HAR 文件转换为 YAML 或 JSON 格式的测试用例,具体转换为什么形式取决于第二个参数(testcase_path)。
$ har2case /path/to/demo.har testcase_path
2.2、若第二个参数为空,则默认转换为 JSON 格式的测试用例,文件名称和路径与 HAR 源文件相同。
$ har2case docs/data/demo-quickstart.har
INFO:root:Generate JSON testcase successfully: docs/data/demo-quickstart.json
2.3、若文件后缀为.yml/.yaml,则转换生成 YAML 格式的测试用例。
$ har2case docs/data/demo-quickstart.har demo.yml
INFO:root:Generate YAML testcase successfully: demo.yml
2.4、若文件后缀为.json,则转换生成 JSON 格式的测试用例。
$ har2case docs/data/demo-quickstart.har demo.json
INFO:root:Generate JSON testcase successfully: demo.json

转换后内容模板如下(需要调整才能运行)

JSON格式


[
{
"config": {
"name": "testcase description",
"variables": [],
"request": {
"base_url": "",
"headers": {
"User-Agent": "python-requests/2.18.4"
}
}
}
},
{
"test": {
"name": "/api/get-token",
"request": {
"url": "http://127.0.0.1:5000/api/get-token",
"headers": {
"device_sn": "FwgRiO7CNA50DSU",
"user_agent": "iOS/10.3",
"os_platform": "ios",
"app_version": "2.8.6",
"Content-Type": "application/json"
},
"method": "POST",
"json": {"sign": "958a05393efef0ac7c0fb80a7eac45e24fd40c27"}
},
"validate": [
{"eq": ["status_code", 200]},
{"eq": ["headers.Content-Type", "application/json"]},
{"eq": ["content.success", true]},
{"eq": ["content.token", "baNLX1zhFYP11Seb"]}
]
}
},
{
"test": {
"name": "/api/users/1000",
"request": {
"url": "http://127.0.0.1:5000/api/users/1000",
"headers": {
"device_sn": "FwgRiO7CNA50DSU",
"token": "baNLX1zhFYP11Seb",
"Content-Type": "application/json"
},
"method": "POST",
"json": {"name": "user1", "password": "123456"}
},
"validate": [
{"eq": ["status_code", 201]},
{"eq": ["headers.Content-Type", "application/json"]},
{"eq": ["content.success", true]},
{"eq": ["content.msg", "user created successfully."]}
]
}
}]

YAML格式

- config:
name: testcase description
parameters:
- user_id: [1001, 1002, 1003, 1004]
variables:
- device_sn: ${gen_random_string(15)}
- user_id: 1000
request:
base_url: http://127.0.0.1:5000
headers:
User-Agent: python-requests/2.18.4
device_sn: $device_sn
Content-Type: application/json

- test:
name: /api/get-token
variables:
- user_agent: "iOS/10.3"
- os_platform: "ios"
- app_version: "2.8.6"
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]

- test:
name: /api/users/$user_id
request:
url: /api/users/$user_id
method: POST
headers:
token: $token
json:
name: user1
password: '123456'
validate:
- eq: [status_code, 201]
- eq: [headers.Content-Type, application/json]
- eq: [content.success, true]
- eq: [content.msg, "user created successfully."]



用例说明
1、权限校验,获取 token
2、支持 CRUD 操作的 RESTful APIs,所有接口的请求头域中都必须包含有效的 token
3、每个 YAML/JSON 文件对应一个测试用例(testcase)
4、每个测试用例为一个list of dict结构,其中可能包含全局配置项(config)和若干个测试步骤(test)
5、config 为全局配置项,作用域为整个测试用例
6、test 对应单个测试步骤,作用域仅限于本身


用例准备已OK,接着用例脚本调试!
原文地址:https://www.cnblogs.com/mys6/p/14768459.html