利用 Postman 中 Tests 断言校验返回结果 您所在的位置:网站首页 postman返回值为空 利用 Postman 中 Tests 断言校验返回结果

利用 Postman 中 Tests 断言校验返回结果

2024-07-16 22:49| 来源: 网络整理| 查看: 265

前言

Postman目前是一款很火的接口测试工具,它有着非常强大结果判断能力。 为什么说强大呢,因为Postman有自带的校验脚本,根本不需要我们去学习JS脚本语言,对于代码能力为0的各位测试小伙伴来说,特别的友好。

通过Tests的代码校验,可以很快的得到结果判断。如果校验通过,则断言为PASS,如果校验失败,则断言为FAIL

Response body:Contains string (校验返回结果中是否包含某个字符串)

代码如下: pm.test("Body matches string", function () { pm.expect(pm.response.text()).to.include("string_you_want_to_search"); });

例子:

结果:

Response body:Is equal to a string (校验返回结果是否等于该字符串)

注意: 这个校验,必须是接口的返回结果与字符串要一模一样。 代码如下: pm.test("Body is correct", function () { pm.response.to.have.body("response_body_string"); });

例子:

结果:

Response body:JSON value check(校验返回结果中某个字段值是否等于某个值)

代码如下: pm.test("Your test name", function () { //设置jsonData变量用来接收postman的json格式的返回数据 var jsonData = pm.response.json(); //判断返回数据中,msg字段是结果是否为OK //此处与需要注意一下json格式,jsonData为整个接口的返回数据,jsonData.msg是第一层级字段 pm.expect(jsonData.value).to.eql(100); }); 例子:

结果:

Response header:Content-type header check(校验响应头是否包含某个值)

代码如下: pm.test("Content-Type is present", function () { pm.response.to.have.header("Content-Type"); });

例子:

结果:

Response time is less than 200ms(校验响应时间是否少于200ms(毫秒))

代码如下: pm.test("Response time is less than 200ms", function () { pm.expect(pm.response.responseTime).to.be.below(200); });

例子PASS:

例子FAIL:

Status code:Code is 200(校验响应头是否包含某个值)

代码如下: pm.test("Status code is 200", function () { pm.response.to.have.status(200); });

例子PASS:

例子FAIL:

至此,Postman断言的多种方式已经学习完毕。加油!!


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有