直接在Tests中写js代码断言结果,Test Result展示运行结果,简单方便。
示例脚本:
var jsonData = JSON.parse(responseBody); var num = jsonData.data.length; if(num > 0){ tests["检查是否返回sku数据"]=true; } else{ tests["检查是否返回sku数据"]=false; } //hot排序 var week2SalesVolume = new Array(num); var flag = true; for(var i=0; i<num; i++){ week2SalesVolume[i]=jsonData.data[i].week2SalesVolume; console.log(week2SalesVolume[i]); } for(var i=0;i<week2SalesVolume.length-1;i++){ for(var j=i+1;j<week2SalesVolume.length;j++){ if(week2SalesVolume[i] < week2SalesVolume[j]){ flag = false; break; } } } if(flag){ tests["检查hot排序顺序"]=true; }else{ tests["检查hot排序顺序"]=false; } if(jsonData.policy=="A"){ tests["检查Query是否走指定版本"]=true; }else{ tests["检查Query是否走指定版本"]=false; }
示例response:
{ "succeed": true, "code": 0, "msg": "success", "total": 19523, "pageNo": 1, "pageSize": 10, "data": [ { "week2SalesVolume": 1226.0 }, { "week2SalesVolume": 1196.0 }, { "week2SalesVolume": 1187.0 }, { "week2SalesVolume": 1159.0 }, { "week2SalesVolume": 1057.0 }, { "week2SalesVolume": 1044.0 }, { "week2SalesVolume": 1039.0 }, { "week2SalesVolume": 979.0 }, { "week2SalesVolume": 963.0 }, { "week2SalesVolume": 913.0 } ], "shopInfo": null, "aggData": {}, "scrollId": null, "policy": "A", "planCode": null, "bucketId": null, "planId": null, "versionId": null, "cookie": null, "point": null }
其他常用脚本:
// 逻辑判断 tests["检查推广置顶sku不被取消"]=jsonData.data[0].goodsSn==="459492001"; tests["检查常规置顶sku不被置顶"]=jsonData.data[1].goodsSn != "231712410"; tests["检查sku实时算法分"] = jsonData.data[0]._scoreLog.details['ai score'] >= 1.0 && jsonData.data[0]._scoreLog.details['ai score']< 100; // 是否包含xx字段 tests["检查是否返回ai score字段"]=responseBody.has("ai score"); if(jsonData.policy=="Z"){ tests["检查是否走指定版本"]=true; }else{ tests["检查是否走指定版本"]=false; } // 常用脚本 # 响应时间必须少于200毫秒 tests["Response time is less than 200ms"] = responseTime < 200; // 状态码200 tests["Status code is 200"] = responseCode.code === 200; // 获取response返回内容 var rsb = responseBody; // 是字符串格式 // 设置环境变量 只能存储字符串,如果是对象的话则无法在下次运行时获取到内容 // 如需要存储JSON数据,可以用JSON.stringify(..)存储,再用JSON.parse(..)转化为对象使用 pm.environment.set("variable_key", "variable_value"); // 获取环境变量 var v = pm.environment.get("variable_key"); // 清除某个环境变量 pm.environment.unset("variable_key"); // 设置全局变量 pm.globals.set("variable_key", "variable_value"); // 获取全局变量和普通变量 var gb = pm.globals.get("variable_key"); var nm = pm.variables.get("variable_key"); // Javascript 获取变量类型 console.log( typeof pm.enviroment );