Postman 常用断言方法

postman常用方法集合:

1.设置环境变量,设置前需要创建并选择对应的变量名

postman.setEnvironmentVariable(“key”, “value”);
pm.environment.get(“key”, “value”);//postman 5.0以上版本设置环境变量的方法

2.设置全局变量

postman.setGlobalVariable(“key”, “value”);
pm.globals.set(“variable_key”, “variable_value”);//postman 5.0以上版本设置全局变量方法

3.检查response body中是否包含某个string

tests[“Body matches string”] = responseBody.has(“string_you_want_to_search”);

pm.test(“Body is correct”, function () {
pm.response.to.have.body(“response_body_string”);
});//5.0以上版本方法

4.检测JSON中的某个值是否等于预期的值

var data = JSON.parse(responseBody);
tests[“Your test name”] = data.value === 100;
JSON.parse()方法,把json字符串转化为对象。parse()会进行json格式的检查是一个安全的函数。

如:检查json中某个数组元素的个数(这里检测programs的长度)

var data = JSON.parse(responseBody);
tests[“program’s lenght”] = data.programs.length === 5;

5.转换XML body为JSON对象

var jsonObject = xml2Json(responseBody);

tests[“Body is correct”] = responseBody === “response_body_string”;

6.检查response body是否与某个string相等

7.测试response Headers中的某个元素是否存在(如:Content-Type)

//getResponseHeader()方法会返回header的值,如果该值存在
tests[“Content-Type is present”] = postman.getResponseHeader(“Content-Type”);

tests[“Content-Type is present”] = responseHeaders.hasOwnProperty(“Content-Type”);
上面的方法,不区分大小写。下面的方法,要区分大小写。

8.验证Status code的值

tests[“Status code is 200”] = responseCode.code === 200;

pm.test(“Status code is 200”, function () {
pm.response.to.have.status(200);
});//5.0以上版本方法

9.验证Response time是否小于某个值

tests[“Response time is less than 200ms”] = responseTime < 200;

//5.0以上版本方法
pm.test(“Response time is less than 200ms”, function () {
pm.expect(pm.response.responseTime).to.be.below(200);
});

10.name是否包含某个值

tests[“Status code name has string”] = responseCode.name.has(“Created”);

//5.0以上版本方法
pm.test(“Status code name has string”, function () {
pm.response.to.have.status(“Created”);
});

11.POST 请求的状态响应码是否是某个值

tests[“Successful POST request”] = responseCode.code === 201 || responseCode.code === 202;

//5.0以上版本方法
pm.test(“Successful POST request”, function () {
pm.expect(pm.response.code).to.be.oneOf([201,202]);
});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值