请求返回结果:
{
"state": 1,
"message": null,
"data": null
}
1、检查状态码:
tests["Status code is 200"] =responseCode.code === 200;
2、检查响应时间:
tests["Response time is less than200ms"] = responseTime < 200;
3、检查body是否含有字段
tests["返回State等于1"] =responseBody.has("\"state\":1");
4、JSON Schema验证
例子1:
var jsonData = JSON.parse(responseBody);
var schema = {
"state": 1,
"message": null,
"data": null
}
tests["json格式验证"] =tv4.validate(jsonData, schema); //验证json格式
tests["state"] =jsonData["state"] == "1";
tests["message"] =jsonData["message"] == null;
tests["data"] =jsonData["data"] == null;:
例子2:
if (responseCode.code === 200){
var jsonData = JSON.parse(responseBody);
var schema = {
"state": 1,
"message": null,
"data": null
}
tests["INFO json格式验证"] = tv4.validate(jsonData, schema); //验证json格式
tests["INFO state"] = jsonData["state"] == "1";
tests["INFO message"] = jsonData["message"] == null;
tests["INFO data"] = jsonData["data"] == null;
console.log(jsonData["state"])
}