Tests
获取环境变量
pm.environment.get("variable_key");
换取全局变量
pm.globals.get("variable_key");
获取变量
pm.variables.get("variable_key");?
设置环境变量
pm.environment.set("variable_key",?"variable_value");?
?设置全局变量
pm.globals.set("variable_key",?"variable_value");
清除环境变量
pm.environment.unset("variable_key");?
发送请求
pm.sendRequest("https://postman-echo.com/get",?function?(err,?response)?{
????console.log(response.json());
});
?校验响应码是否为200
pm.test("Status?code?is?200",?function?()?{
????pm.response.to.have.status(200);
});
校验响应文本是否包含字符串
pm.test("Body?matches?string",?function?()?{
????pm.expect(pm.response.text()).to.include("string_you_want_to_search");
});
?如:
pm.test("Body?matches?string",?function?()?{
????pm.expect(pm.response.text()).to.include("access_token");
});
判断响应文本中有access_token字段
检验响应json的某个值
pm.test("Your?test?name",?function?()?{
????var?jsonData?=?pm.response.json();
????pm.expect(jsonData.value).to.eql(100);
});
?如判断响应errcode的值是否为0
pm.test("Your?test?name",?function?()?{
????var?jsonData?=?pm.response.json();
????pm.expect(jsonData.errcode).to.eql(0);
});
校验响应体
pm.test("Body?is?correct",?function?()?{
????pm.response.to.have.body("response_body_string");
});
?返回内容必须跟预期完全相等。
校验响应头
pm.test("Content-Type?is?present",?function?()?{
????pm.response.to.have.header("Content-Type");
});
校验响应时间是否少于200ms
pm.test("Response?time?is?less?than?200ms",?function?()?{
????pm.expect(pm.response.responseTime).to.be.below(200);
});
|