postman断言语句使用

本文介绍了在Postman中进行接口测试时使用的多种断言语句,包括检查状态码、响应体内容、Header信息、响应时间以及设置和管理变量等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近在使用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");
});
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值