Tests模块方法
请求地址
https://ke.qq.com/cgi-bin/comment_new/course_comment_stat?cid=399017&bkn=1052095361&r=0.9776743212243275
环境变量
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| pm.environment.set("test01","hhhhh");
var array = [1,2,3,4]; pm.environment.set("array",JSON.stringify(array, null, 2)); var obj = {a:[1,2,3],b:{c:'val'}}; pm.environment.set("obj",JSON.stringify(obj))
console.log(pm.environment.get("obj")); console.log(JSON.parse(pm.environment.get("array"))); console.log(JSON.parse(pm.environment.get("obj")));
pm.environment.unset("obj");
|
全局变量
1 2 3 4 5 6 7 8
| pm.globals.set("test02","ddddd");
console.log(`全局变量:${pm.globals.get("test02")}`);
pm.globals.unset('test02');
|
variables 变量
1 2 3 4 5 6 7 8
| console.log(pm.variables.get("array"));
pm.variables.set("test03","qqqqqq"); console.log(pm.variables.get("test03")); pm.variables.unset("test03"); console.log(pm.variables.get("test03"));
|
response断言
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| pm.test("检查响应主体是否包含字符串",function(){ stringData = pm.response.text(); pm.expect(stringData).to.include("result"); });
pm.test("断言JSON值",function(){ var jsonData = pm.response.json(); pm.expect(jsonData.result.good_num).to.eql(214); });
pm.test("断言返回时间",function(){ var time = pm.response.responseTime; pm.expect(time).to.be.below(200); });
pm.test("成功的POST请求状态码", function () { var code = pm.response.code; pm.expect(code).to.be.oneOf([200, 201, 202, 203]); });
|
to.have断言
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| pm.test("检查响应体是否等于字符串",function(){ pm.response.to.have.body("{\"result\":{\"good_num\":214,\"medium_num\":0,\"bad_num\":2,\"id\":399017,\"all_num\":216,\"good_percentage\":99},\"retcode\":0}") });
pm.test("Content-Type是否存在",function(){ pm.response.to.have.header("Content-Type"); });
pm.test("断言状态码",function(){ pm.response.to.have.status(200); });
pm.test("代码名包含一个字符串", function () { pm.response.to.have.status("ok"); });
|
功能
1 2 3 4 5 6 7
| pm.sendRequest("192.168.68.251/bsams/front/vericode.do", function (err, response) { console.log(response); });
var jsonObject = xml2Json(responseBody);
|