如何验证邮递员的回复正文?

时间:2018-07-13 11:24:19

标签: postman

如果只得到这个主体,则需要通过测试用例。

{
    "statuscode": "200",
    "res": [{
        "bwInfo": {
            "timeStamp": {
                "seconds": 0,
                "nanoSeconds": 0
            },
            "appIns_Id": "string",
            "requestType": "string",
            "sessionFilter": [{
                "sourceIP": "string",
                "sourcePort": [
                    "string"
                ],
                "destAddress": "string",
                "dstPort": [
                    "string"
                ],
                "protocol": "HTTPS"
            }],
            "fixedBWPriority": "1",
            "fixedAllocation": "100",
            "allocationDirection": "00"
        }
    }]
}

我为验证编写的代码是:

 var jsonData = pm.response.json()
 if (jsonData !== null) {
     pm.test("isString", function() {
         for (i = 0; i < jsonData.length; i++) {
             pm.expect(jsonData[i]['bwInfo']['appIns_Id']).to.be.a('string');
             pm.expect(jsonData[i]['bwInfo']['fixedAllocation']).to.be.a('string');
             pm.expect(jsonData[i]['bwInfo']['allocationDirection']).to.be.a('string');
             pm.expect(jsonData[i]['bwInfo']['sessionFilter']['sourceIP']).to.be.a('string');
             pm.expect(jsonData[i]['bwInfo']['sessionFilter']['sourcePort']).to.be.a('string');
             pm.expect(jsonData[i]['bwInfo']['sessionFilter']['destAddress']).to.be.a('string');
             pm.expect(jsonData[i]['bwInfo']['sessionFilter']['dstPort']).to.be.a('string');
             pm.expect(jsonData[i]['bwInfo']['sessionFilter']['protocol']).to.be.a('string');
             pm.expect(jsonData[i]['bwInfo']['requestType']).to.be.a('string');
             pm.expect(jsonData[i]['bwInfo']['fixedBWPriority']).to.be.a('string');
             pm.expect(jsonData[i]['bwInfo']['timeStamp']).to.be.a('string');
         }
     });
 } else {
     pm.test("Not found", function() {

     });
 }

我只希望测试用例在状态码为200时进行验证,但是无论即将出现什么状态码,它仍然可以通过。请引导我。

2 个答案:

答案 0 :(得分:1)

摘录下面的代码片段将帮助您检查响应状态代码。

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

来源:http://blog.getpostman.com/2017/10/25/writing-tests-in-postman/

答案 1 :(得分:1)

您可以使用以下代码段验证任何状态代码。只需修改脚本中的状态代码编号即可。 我使用 pm.expect 和 pm.response 以两种不同的方式编写。根据您的方便使用其中任何一个。

    // To verify if the status code is  200
    pm.test("Status code is 200", function () {
        // Using Response Assertion API
    pm.response.to.have.status(200);
        // Using explicit expect
    pm.expect(pm.response.code).to.eql(200);
    });
相关问题