postman常用断言

1.检查请求后返回的状态码 status 为200

备注:status code:Code is 200

pm.test(“Status code is 200”, function () {

pm.response.to.have.status(200);

});

2.通过返回状态码检查是成功的post请求

备注:status code:successful POST request

pm.test(“Successful POST request”, function () {

pm.expect(pm.response.code).to.be.oneOf([201,202]);

});

3.检查response body等于指定字符串

备注:response body:is equal to a string

pm.test(“Body is correct”, function () {

pm.response.to.have.body("response_body_string");

});

4.检查response body中包含字符串

备注:response body:contains string

pm.test(“Body matches string”, function () {

pm.expect(pm.response.text()).to.include("string_you_want_to_search");

});

5.检查content-Type是否包含在header返回

备注:response headers:content-Type header check

pm.test(“Content-Type is present”, function () {

pm.response.to.have.header("Content-Type");

});

6.检查response body中JSON某个字段值

备注:response body:JSON value check

pm.test(“Your test name”, function () {

var jsonData = pm.response.json();

pm.expect(jsonData.value).to.eql(100);

});

7.检查响应时间超过200ms

备注:response time is than 200ms

pm.test(“Response time is less than 200ms”, function () {

pm.expect(pm.response.responseTime).to.be.below(200);

});

8.response body:将xml转换为JSON对象

备注:response body:convert XML body to a JSON object

var jsonObject = xml2Json(responseBody);

9.设置一个环境变量

备注:set an environment variable

pm.environment.set(“variable_key”, “variable_value”);

10.设置一个全局变量

备注:set a global variable

pm.globals.set(“variable_key”, “variable_value”);

11.清除全局变量

备注:clear a global variable

pm.globals.unset(“variable_key”);

12.清除环境变量

备注:Clear an environment variable

pm.environment.unset(“variable_key”);

13.获取一个全局变量

备注:get a global variable

pm.globals.get(“variable_key”);

14.获得一个环境变量

备注:Get an environment variable

pm.environment.get(“variable_key”);

15.获得一个变量

备注:get a variable

pm.variables.get(“variable_key”);

16.检查返回状态码中有指定字符串

备注:status code:code name has string

pm.test(“Status code name has string”, function () {

pm.response.to.have.status("Created");

});

17.发送一个请求

备注:send s request

pm.sendRequest(“https://postman-echo.com/get”, function (err, response) {

console.log(resp  onse.json());

});

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Postman常用断言包括状态码断言、响应正文断言(包含文本、JSON字段)、响应头断言和响应时间断言等。 状态码断言用于验证接口返回的HTTP状态码是否符合预期。可以通过在测试脚本使用`pm.response.to.have.status(statusCode)`来实现,其`statusCode`是预期的状态码。 响应正文断言是用来验证响应正文的内容是否符合预期。可以使用不同的方法进行断言,比如: - 包含断言:使用`pm.expect(responseBody).to.include(expectedText)`,其`responseBody`是响应正文,`expectedText`是预期的文本内容。 - JSON字段断言:使用`pm.expect(jsonData.property).to.eql(expectedValue)`,其`jsonData.property`是响应正文的JSON字段,`expectedValue`是预期的值。 响应头断言用于验证响应头的信息是否符合预期。可以使用`pm.response.to.have.header(headerName, expectedValue)`来实现,其`headerName`是预期的响应头名称,`expectedValue`是预期的值。 响应时间断言用于验证接口的响应时间是否在预期范围内。可以使用`pm.expect(duration).to.be.below(maximumDuration)`来实现,其`duration`是接口的实际响应时间,`maximumDuration`是预期的最大响应时间。 这些断言可以帮助我们验证接口的正确性和稳定性,确保接口的返回结果符合预期。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [postman7种断言的使用](https://blog.csdn.net/qq_43479892/article/details/127171379)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Postman-win64-Setup.rar](https://download.csdn.net/download/dengjin20104042056/88026048)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值