https://www.cnblogs.com/AmilyWilly/p/9512554.html
返回结果
[
{
"total": 5,
"success": 4,
"uploadId": 508
}
]
TEST方法编写:
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
pm.expect(pm.response.text()).to.include("success");
var jsonData = JSON.parse(responseBody);
var res = jsonData[0].uploadId;
var exceptvalue = pm.environment.get("uploadId");
pm.expect(res).to.equal(parseInt(exceptvalue));
});
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
pm.expect(pm.response.text()).to.include("210321");
});
获取全局变量有两种方式:
1)请求参数中可以直接获取:{{var_name}}
2)代码中获取:var value = pm.globals.get("var_name");
获取环境变量有两种方式:
1)请求参数中可以直接获取:{{var_name}}
2)代码中获取:var value = pm.environment.get("var_name");
postman的执行顺序是按照放置的位置顺序执行的
var jsonData = JSON.parse(responseBody);
var resp_token = jsonData.result.info.token;
postman.setEnvironmentVariable("token", resp_token);
tests["response code is 200"] = jsonData.result.code === 200;
http://blog.csdn.net/wanglha/article/details/52203736
postman变量使用
postman环境变量使用
var jsonData = JSON.parse(responseBody);
var resp_token = jsonData.result.info.token;
postman.setEnvironmentVariable("token", resp_token);
tests["response code is 200"] = jsonData.result.code === 200;
引用变量时直接输入{{变量名称}}
接口调用接口,数据存储到环境变量再去调用
JSON数据结构验证
返回内容
{
"result" : {
"code" : "200",
"msg" : "操作成功",
"info" : {
"usrKey" : "1042907",
"usrMobile" : "15980972025",
"usrName" : "meiying613"
}
}
}
实例:订单--确认收货
var schema ={
"type":"object",
"required":["result"],
"properties":{
"result":{ "$ref" : "#/definitions/result" },
},
"definitions":{
"result":{
"type":"object",
"required":["code","msg","info"],
"properties":{
"code":{"type" : "string"},
"msg":{"type" : "string"},
"info":{"$ref" : "#/definitions/info"},
},
"definitions":{
"info":{
"type":"object",
"required":["ctmUsrKey"],
"properties":{
"ctmUsrKey":{"type" : "string"},
},
}
}
}
}
}
//console.log(tv4.error);
var jsonData = JSON.parse(responseBody);
tests["Valid schema"] = tv4.validate(jsonData, schema);
tests["Valid ctmUsrKey"] = jsonData.result.info.ctmUsrKey==="1069310";
tests["Valid msg"] = jsonData.result.msg==="操作成功";
tests["Valid code"] = responseCode.code===200;
别人的实例:
var responseJSON;
try {
responseJSON = JSON.parse(responseBody);
tests["Status equals 200"] = responseJSON.status === "OK";
}
catch (e) { }
tests["Status code is 200"] = responseCode.code === 200;
var jsonData = JSON.parse(responseBody);
tests["sms.send.status"] = jsonData.status === "OK";
tests["responseBody has message"]=jsonData.result.message === "success"
var jsonData = JSON.parse(responseBody);
if (jsonData.error){
tests["Body has sign"] = jsonData.error.msg="无效的签名";
postman.setEnvironmentVariable("sign", jsonData.error.sign);
}
else {
tests["Body has sign"] = true;
}
返回格式校验
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
pm.test("返回报文格式校验", function () {
var schema = {
"type":"object",
"properties":
{
"id":{"type": "number"},
"externalId":{"type": "string"},
"type":{"type": "string"},
"placeOfAction":{"type": "string"},
"description":{"type": "string"},
"status":{"type": "string"},
"typeIcon":{"type": "string"},
"triggeringDevice":{"type": "string"},
"creationDate":{"type": "string"}
},
"required":true
};
pm.response.to.have.jsonSchema(schema);
});
pm.environment.set("eventId", pm.response.json().id);
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
var jsonData = JSON.parse(responseBody);
pm.environment.set("id_0", jsonData.id);
pm.environment.set("id_1", jsonData.id);
});