在使用postman做接口測試的時候运嗜,可以在tests中對該接口進行斷言設置
json字符串解析斷言
上面的圖為使用postman斷言方法之一歹撒,使json解析key的值進行校驗
tests["Your test name"] = jsonData.value === 100;
其中 Your test name 為斷言名,jsonData.value表示為對json進行解析,=== 100 表示為key的value值
{
"ok": true,
"data": {
"user": {
"user_no": "888",
"type": "1",
"user_name": "W HE",
"fullname": "W HE",
"nickname": "",
"login_name": "1258@qq.com",
"headimg": "",
"is_payed": true,
"is_certified": false
},
"redirect_url": "/team/search"
}
}
jsonData["data"]["user"]["user_name"] === "W HE"
附:
常用的postman斷言 | 解釋 | 對應腳本 |
---|---|---|
Response body:Contains string | response包含字符串 | tests["Body matches string"] = responseBody.has("string_you_want_to_search"); |
Response body:Is equal to a string | response body等于指定字符串 | tests["Body is correct"] = responseBody === "response_body_string"; |
response body:JSON value check | json解析key的值進行校驗 | tests["Your test name"] = jsonData.value === 100; |
status code:Code is 200 | 判斷狀態(tài)碼 | tests["Status code is 200"] = responseCode.code === 200; |
status code:code name has string | 檢查code name 是否包含內(nèi)容 | tests["Status code name has string"] = responseCode.name.has("Created"); |