1.extract关键字传递参数
运行官网api-server.py
在测试步骤(test)中,若需要从响应结果中提取参数,则可使用 extract
关键字。extract 的列表中可指定一个或多个需要提取的参数。
在提取参数时,当 HTTP 的请求响应结果为 JSON 格式,则可以采用.
运算符的方式,逐级往下获取到参数值;响应结果的整体内容引用方式为 content 或者 body。
如响应结果为:
{"success": true, "token": "ZQkYhbaQ6q8UFFNE"}
则可以:
extract:
- token: content.token
example:
- config:
name: testcase description
variables: {}
- test:
name: /api/get-token
request:
headers:
Content-Type: application/json
User-Agent: python-requests/2.18.4
app_version: 2.8.6
device_sn: FwgRiO7CNA50DSU
os_platform: ios
json:
sign: 9c0c7e51c91ae963c833a4ccbab8d683c4a90c98
method: POST
url: http://127.0.0.1:5000/api/get-token
validate:
- eq: [status_code, 200]
- eq: [headers.Content-Type, application/json]
- eq: [content.success, true]
# - eq: [content.token, baNLX1zhFYP11Seb]
extract:
- token: content.token
- test:
name: /api/users/1000
request:
headers:
Content-Type: application/json
User-Agent: python-requests/2.18.4
device_sn: FwgRiO7CNA50DSU
token: $token
json:
name: user1
password: '123456'
method: POST
url: http://127.0.0.1:5000/api/users/1000
validate:
- eq: [status_code, 201]
- eq: [headers.Content-Type, application/json]
- eq: [content.success, true]
- eq: [content.msg, user created successfully.]
2.base_url的优化
上述例子中的url,都是host+path格式的,基本test中的host都是一致的,所以可以提取出来,放到config中
注意:base_url是request下的一个参数,所以必须放在request下,否则会报错找不到base_url
- config:
name: testcase description
request:
base_url: http://127.0.0.1:5000 #base_url必须放在request下
variables: {}
- test:
name: /api/get-token
request:
headers:
Content-Type: application/json
User-Agent: python-requests/2.18.4
app_version: 2.8.6
device_sn: FwgRiO7CNA50DSU
os_platform: ios
json:
sign: 9c0c7e51c91ae963c833a4ccbab8d683c4a90c98
method: POST
url: /api/get-token
validate:
- eq: [status_code, 200]
- eq: [headers.Content-Type, application/json]
- eq: [content.success, true]
# - eq: [content.token, baNLX1zhFYP11Seb]
extract:
- token: content.token
- test:
name: /api/users/1000
request:
headers:
Content-Type: application/json
User-Agent: python-requests/2.18.4
device_sn: FwgRiO7CNA50DSU
token: $token
json:
name: user1
password: '123456'
method: POST
url: /api/users/1000
validate:
- eq: [status_code, 201]
- eq: [headers.Content-Type, application/json]
- eq: [content.success, true]
- eq: [content.msg, user created successfully.]
3.varies变量声明与引用
varies主要用来指定变量,分为test下的varies和config下的varies,config下的变量全局通用,test下的只能局部使用
- config:
name: testcase description
request:
base_url: http://127.0.0.1:5000
variables:
username: user1
password: 123456
- test:
name: /api/get-token
request:
headers:
Content-Type: application/json
User-Agent: python-requests/2.18.4
app_version: 2.8.6
device_sn: FwgRiO7CNA50DSU
os_platform: ios
json:
sign: 9c0c7e51c91ae963c833a4ccbab8d683c4a90c98
method: POST
url: /api/get-token
validate:
- eq: [status_code, 200]
- eq: [headers.Content-Type, application/json]
- eq: [content.success, true]
# - eq: [content.token, baNLX1zhFYP11Seb]
extract:
- token: content.token
- test:
name: /api/users/1000
request:
headers:
Content-Type: application/json
User-Agent: python-requests/2.18.4
device_sn: FwgRiO7CNA50DSU
token: $token
json:
name: $username
password: $password
method: POST
url: /api/users/1000
validate:
- eq: [status_code, 201]
- eq: [headers.Content-Type, application/json]
- eq: [content.success, true]
- eq: [content.msg, user created successfully.]