postman 测试多级数组中的值

wxclj1h5  于 2022-11-07  发布在  Postman
关注(0)|答案(1)|浏览(168)

我正在将一个测试从ReadyAPI转换为Postman,并且正在努力应对这个场景(我是新手,没有经验)。从json返回了一个多级数组(下面的示例片段)。ReadyAPI测试非常简单,因为它检查特定值的存在性。具体来说,检查XML,如下所示:

<assertionOfType="JsonPath Existence Match", name="Check for existence of [ilm]"

我的问题是我如何在 Postman 中做到这一点。

  • 返回的json示例:*
"t712714"" {
   "ilm": {
       "hadr": {
           "clusterName": "t712714",
           "clusterType": "hadr",
           "dataCenter": "ilm",
           "extractedTs": "07/09/2022 12:15:07 AM UTC",
           "bindState": false,
           "lockState": true,
           "databaseCount": 29,
           "insertionAgeMins": 4,
           "onlineCount": 29,
           "activeServers": ["testudb602l","testudb604l"],
           "inactiveServers": ["testudb603l","testudb605l"],
       "qrep": {
           "clusterName": "t712714",
           "clusterType": "hadr",
eqqqjvef

eqqqjvef1#

这就是我最后所做的,测试a)200响应,B)返回特定的群集名称,c)确保返回群集名称ILM。

var resp = JSON.parse(responseBody);

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

pm.test("Response contains the right properties", function() {
    let respBody = JSON.parse(responseBody);
    pm.expect(respBody.t712714).to.have.property("dfw")
})

pm.test('Ensure data center is returned', function() {
    let dataCenter = "dataCenter";

    _.forEach(resp, (respObj, index) => {
        if (respObj.dataCenter === 'ilm') {
            console.log('Found', respObj);
        }
    });
});

相关问题