最近在使用postman工具对接口进行测试,针对目前对接口中验证的内容不同,使用的断言语句不同。
1.检查请求后返回的状态码 status 为200
pm.test("Status code is 200", function () {
pm.response.to.have.status("200");
});
2.通过返回状态码检查是成功的post请求
pm.test("Successful POST request", function () {
pm.expect(pm.response.code).to.be.oneOf([201,202]);
});
3.检查response body等于指定字符串
pm.test("Body is correct", function () {
pm.response.to.have.body("指定字符串");
});
4.检查response body中包含字符串
pm.test("Body matches string", function () {
pm.expect(pm.response.text()).to.include("包含的字符串");
});
5.检查content-Type是否包含在header返回
pm.test("Content-Type is present", function () {
pm.response.to.have.header("包含的字符串");
});
6.检查response body中JSON某个字段值
pm.test("Your test name", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.value).to.eql(100);
});
7.检查响应时间超过200ms
pm.test("Response time is less than 200ms", function () {
pm.expect(pm.response.responseTime).to.be.below("200");
});
8.response body:将xml转换为JSON对象
var jsonObject = xml2Json(responseBody);
9.设置一个环境变量
pm.environment.set("variable_key", "variable_value");
10.设置一个全局变量
pm.globals.set("variable_key", "variable_value");
11.清除全局变量
pm.globals.unset("variable_key");
12.清除环境变量
pm.environment.unset("variable_key");
13.获取一个全局变量
pm.globals.get("variable_key");
14.获得一个环境变量
pm.environment.get("variable_key");
15.获得一个变量
pm.variables.get("variable_key");
16.检查返回状态码中有指定字符串
pm.test("Status code name has string", function () {
pm.response.to.have.status("Created");
});