变量
使用$
开头的字符串,被当做变量.
例如:$ user_agent
将不会被当作一个普通的字符串,HttpRunner会把它看作一个名为user_agent
的变量,搜索并返回它的绑定值。
方法
引用一个Python方法时,将使用$ {}
。$ {}
中的任何内容将被视为方法调用。
同时,变量也可以作为方法的参数被引用。
例如:
json:
sign: ${get_sign($user_agent, $device_sn, $os_platform, $app_version)}
这里sign的值就是get_sign
方法的返回值. 其中($user_agent, $device_sn, $os_platform, $app_version)
是传入的参数.
公共参数
可以用config
模块 提取出一个.yaml文件
的全局变量,可以在文件内的所有test_case有效.
在配置块中设置base_url,从而可以在每个API的request url中指定相对路径。此外,还可以在配置请求中设置通用字段,比如header中的device_sn。
例如:
- config:
name: "smoketest for CRUD users."
variables:
- device_sn: ${gen_random_string(15)}
request:
base_url: http://127.0.0.1:5000
headers:
device_sn: $device_sn
- test:
name: get token
variables:
- user_agent: 'iOS/10.3'
- os_platform: 'ios'
- app_version: '2.8.6'
request:
url: /api/get-token
method: POST
headers:
user_agent: $user_agent
os_platform: $os_platform
app_version: $app_version
json:
sign: ${get_sign($user_agent, $device_sn, $os_platform, $app_version)}
extract:
- token: content.token
validate:
- {"check": "status_code", "comparator": "eq", "expect": 200}
- {"check": "content.token", "comparator": "len_eq", "expect": 16}
- test:
name: create user which does not exist
request:
url: /api/users/1000
method: POST
headers:
token: $token
json:
name: "user1"
password: "123456"
validate:
- {"check": "status_code", "comparator": "eq", "expect": 201}
- {"check": "content.success", "comparator": "eq", "expect": true}
提取和校验返回报文的字段
有返回报文如下:
// status code: 200
// response headers
{
"Content-Type": "application/json"
}
// response body content
{
"success": False,
"person": {
"name": {
"first_name": "Leo",
"last_name": "Lee",
},
"age": 29,
"cities": ["Guangzhou", "Shenzhen"]
}
}
如果要获取响应头里面的 Content-Type 字段,可以使用headers.content-type
.
如果要获取响应正文里的first_name,可以用content.person.name.first_name
.
获取list的数据,可以使用其所在的下标.例如,要获取响应正文里的Guangzhou ,可以用content.person.cities.0
.
extract:
- content_type: headers.content-type
- first_name: content.person.name.first_name
validate:
- eq: ["status_code", 200]
- eq: ["headers.content-type", "application/json"]
- gt: ["headers.content-length", 40]
- eq: ["content.success", true]
- len_eq: ["content.token", 16]
网友评论