目录
1.对接口进行定义
1.1编写接口定义
可以先将案例目录tests\api中的get_token.yml拷贝到自己构建的测试结构目录中,Api_api\api,加一些备注,也好知道以后如何照着例子写:
name: get token #接口定义描述
base_url: http://127.0.0.1:5000 #请求的根目录
variables: #定义变量
user_agent: PostmanRuntime/7.24.0
device_sn: ${gen_random_string(15)}
os_platform: ios
app_version: 2.8.6
expected_status_code: 200
request: #请求信息
url: /api/get-token #请求的相对路径
method: POST
headers:
user_agent: $user_agent
device_sn: $device_sn
os_platform: $os_platform
app_version: $app_version
Content-Type: "application/json"
device_sn: $device_sn
json:
sign: ${get_sign($device_sn, $os_platform, $app_version)}
validate: #校验/断言
- eq: ["status_code", $expected_status_code] #状态码
- len_eq: ["content.token", 16] #token字符串长度
- contains: [{"a": 1, "b": 2}, "a"] #此句不明何意?
1.2调试API
API 尽量保持完整,可以单独运行,方便调试。
hrun D:\httprunner\Api_api\api\get_token.yml
2.测试用例调用已定义的API
2.1引用 接口定义
config:
name: 获得 token #必填,测试用例名称,测试报告以此作为标题
variables: #非必填,全局变量,作用域为整个testcase,可合并覆盖API中的variables
user_agent: win/7
os_platform: 'win'
device_sn: TESTCASE_SETUP_001
teststeps:
- name: /api/get-token #必填,测试步骤名称
api: api/get_token.yml
variables: #合并覆盖API中的variables,但不能覆盖config中的variables
user_agent: 'iOS/12'
app_version: '7'
extract:
- session_token: content.token
teardown_hooks:
- ${hook_print($user_agent)}
- ${hook_print($os_platform)}
- ${hoo