postman之断言

postman Tests断言

在 postman 中封装了我们常见的断言(Tests),Tests 除了可以作为断言,还可以当做后置处理器,经常应用于:

  • 获取当前接口的响应,传递给下一个接口
  • 控制多个接口间的执行顺序。

postman之断言

 

其中以下 8 种与预置脚本中的一致,这些严格意义上并不是断言而是辅助进行参数化的,与预置脚本相比唯一的不同在于生效时间,预置脚本在请求前生效,断言在请求后生效。

  • pm.environment.get("variable_key");
  • pm.globals.get("variable_key");
  • pm.variables.get("variable_key");
  • pm.environment.set("variable_key", "variable_value");
  • pm.globals.set("variable_key", "variable_value");
  • pm.environment.unset("variable_key");
  • pm.globals.unset("variable_key");
  • pm.sendRequest("https://postman-echo.com/get", function (err, response) { console.log(response.json());});

除去上面 8 种断言外,其他断言的含义如下:

案例:

  1. 请求地址:https://{{IP}}/cgi-bin/tags/get?access_token={{access_token}}(微信公众号-获取公众号已创建的标签
  2. 请求方式:https
  3. 请求方式:get
  4. 参数:access_token
  5. 响应数据
    {
        "tags": [
            {
                "id": 2,
                "name": "星标组",
                "count": 0
            },
            {
                "id": 100,
                "name": "学习1605677877",
                "count": 0
            },
            {
                "id": 101,
                "name": "我们的乔治20来斤1111111",
                "count": 0
            },
            {
                "id": 104,
                "name": "学习",
                "count": 0
            },
            {
                "id": 105,
                "name": "学习0",
                "count": 0
            },
            {
                "id": 106,
                "name": "学习01",
                "count": 0
            },
            {
                "id": 107,
                "name": "学习0100",
                "count": 0
            },
            {
                "id": 108,
                "name": "学习11.18",
                "count": 0
            },
            {
                "id": 109,
                "name": "学习11.19",
                "count": 0
            },
            {
                "id": 110,
                "name": "学习1605677648",
                "count": 0
            },
            {
                "id": 111,
                "name": "学习1605677654",
                "count": 0
            },
            {
                "id": 112,
                "name": "学习1605677734",
                "count": 0
            },
            {
                "id": 113,
                "name": "学习1605677765",
                "count": 0
            },
            {
                "id": 114,
                "name": "学习1605677831",
                "count": 0
            },
            {
                "id": 116,
                "name": "东广东广东广东",
                "count": 0
            },
            {
                "id": 117,
                "name": "学习1605681456",
                "count": 0
            },
            {
                "id": 118,
                "name": "学习1605683299",
                "count": 0
            },
            {
                "id": 119,
                "name": "学习1605683312",
                "count": 0
            },
            {
                "id": 120,
                "name": "学习1605683559",
                "count": 0
            },
            {
                "id": 121,
                "name": "学习1605684354",
                "count": 0
            },
            {
                "id": 122,
                "name": "学习00",
                "count": 0
            },
            {
                "id": 123,
                "name": "学习1605685073587",
                "count": 0
            },
            {
                "id": 124,
                "name": "学习1605687683589",
                "count": 0
            },
            {
                "id": 125,
                "name": "学习1605687964717",
                "count": 0
            },
            {
                "id": 126,
                "name": "学习1605687980897",
                "count": 0
            },
            {
                "id": 127,
                "name": "学习1605688063677",
                "count": 0
            },
            {
                "id": 129,
                "name": "学习1605688889250",
                "count": 0
            },
            {
                "id": 132,
                "name": "学习1605751457778",
                "count": 0
            },
            {
                "id": 135,
                "name": "学习1605944133535",
                "count": 0
            },
            {
                "id": 136,
                "name": "学习1605944458899",
                "count": 0
            },
            {
                "id": 140,
                "name": "??",
                "count": 0
            },
            {
                "id": 141,
                "name": "???",
                "count": 0
            },
            {
                "id": 142,
                "name": "????",
                "count": 0
            },
            {
                "id": 143,
                "name": "2021",
                "count": 0
            },
            {
                "id": 144,
                "name": "????3",
                "count": 0
            },
            {
                "id": 145,
                "name": "????4",
                "count": 0
            },
            {
                "id": 146,
                "name": "people",
                "count": 0
            },
            {
                "id": 147,
                "name": "people1",
                "count": 0
            },
            {
                "id": 148,
                "name": "people2",
                "count": 0
            },
            {
                "id": 149,
                "name": "people3",
                "count": 0
            },
            {
                "id": 150,
                "name": "??000",
                "count": 0
            },
            {
                "id": 151,
                "name": "??0001",
                "count": 0
            },
            {
                "id": 152,
                "name": "??00010",
                "count": 0
            },
            {
                "id": 153,
                "name": "??000100",
                "count": 0
            },
            {
                "id": 154,
                "name": "happy",
                "count": 0
            },
            {
                "id": 155,
                "name": "happy new",
                "count": 0
            },
            {
                "id": 156,
                "name": "happy new year",
                "count": 0
            },
            {
                "id": 157,
                "name": "happy new year?",
                "count": 0
            },
            {
                "id": 158,
                "name": "happy new year2012",
                "count": 0
            },
            {
                "id": 159,
                "name": "happy new year 2012",
                "count": 0
            },
            {
                "id": 160,
                "name": "happy new year 2021",
                "count": 0
            },
            {
                "id": 161,
                "name": "happy new year 202222",
                "count": 0
            }
            
            
        ]
    }

     

  • 状态断言

//1.状态码返回为200

pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
}); 

Tests

postman之断言

查看断言结果

postman之断言

  • 业务断言

//2.断言返回的结果中包括一个指定字符串---精确断言

pm.test("Body 中包含tags", function () {

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

});

Tests

postman之断言

查看断言结果

postman之断言

//3.对返回的结果做json字段检查

pm.test("name = 学习1605677877", function () {

    var jsonData = pm.response.json();

    pm.expect(jsonData.tags[1].name).to.eql("学习1605677877");

});

Tests

postman之断言

查看断言结果

postman之断言

//4.断言实际获取的响应体(即 Body 信息)与预期结果的响应体是否一致

//pm.test("Body is correct", function () {

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

//});

Tests

postman之断言

查看断言结果

postman之断言

//5.响应头中包含有指定的响应头

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

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

});

Tests

postman之断言

查看断言结果

postman之断言

//6.断言接口请求的时间少于200ms

pm.test("Response time is less than 300ms", function () {

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

});

Tests

postman之断言

查看断言结果

postman之断言

//7.断言一个post请求的返回状态码是否在指定的范围中

pm.test("Successful POST request", function () {

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

});

Tests

postman之断言

查看断言结果

postman之断言

//8.断言返回的状态码信息中是否包含指定字段

pm.test("Status code包含OK", function () {

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

});

Tests

postman之断言

查看断言结果

postman之断言

需要注意的点:postman 获取断言时,任何响应都必须转为 JsonData 对象。例如 我们想获取如下响应信息中键值为 name 的值:

postman之断言

此时我们这样做:

postman之断言

  • 第一步定义一个jsonData对象,将响应值付给jsonData
  • var jsonData = pm.response.json();
  • 第二步通过“.”获取到我们想要的name值
  • var name= jsonData.tags[1].name

由此我们可以任意获取响应中的字段,进而在设置成相应的环境变量,当然就可以作为参数传递给任意接口了,这就是接口间参数传递的原理所在

上一篇:接口测试之初步认识Postman


下一篇:7组-Beta冲刺-2/5