【问题标题】:Verify the test for json output in postman for arraylist在 postman 中验证对 json 输出的测试以获取 arraylist
【发布时间】:2023-03-07 01:16:01
【问题描述】:

有一个没有任何消息的 json 输出。如何仅验证用户特定值的测试?因为我希望我的测试验证用户、名字、角色和办公室数量

{
    "Status": "OK",
    "Message": "",
    "users": [
        {
            "address": {
                "city": "test",
                "state": "CL",
                "zip": "60585"
            },
            "mobile": "",
            "userId": "789455061",
            "email": "test@abc.com",
            "enabled": true,
            "fax": "",
            "firstName": "fortest",
            "id": "6ec-asd-132",
            "lastName": "test",
            "licensedStates": [],
            "connectid": "123456",
            "organizations": [
                {
                    "OrgID": "110465",
                    "offices": [
                        {
                            "OrgID": "11465",
                            "address": {
                                "city": "newcity",
                                "state": "BL",
                                "zip": "60585"
                            },
                            "id": "",
                            "name": "Test Data Pvt LTd"
                        }
                    ],
                    "id": "789434",
                    "name": "Test Data Pvt Ltd"
                }
            ],
            "phone": "",
            "roles": {
                "Tester": true,
                "Dev": false,
                "Manager": false
            },
            "suffix": "",
            "title": ""
        }
    ]
}

将响应 json 声明为变量。然后尝试了,然后也需要角色。

pm.test("testfordata",responsejson.users[0].enabled.is.equal(true);

错误:无法读取 'undefined' 的属性 'equal'

【问题讨论】:

  • 贴出实际代码,请不要截图。
  • 希望代码有帮助?我正在寻找用户和角色列表测试请分享输入
  • 未格式化时不在 cmets 中。只需编辑问题并将其添加到代码块中即可。
  • 编辑完成请分享输入

标签: arrays json testing postman


【解决方案1】:

这是一个非常有针对性的测试,仅基于上面的数据集,不考虑检查users数组中的第一个对象:

pm.test("testfordata", function () {
    let jsonData = pm.response.json();
    pm.expect(jsonData.users[0].enabled).to.be.true
    pm.expect(jsonData.users[0].firstName).to.equal("fortest")
    pm.expect(jsonData.users[0].roles).to.have.keys(["Tester", "Dev", "Manager"])
    pm.expect(jsonData.users[0].organizations[0].offices.length).to.equal(1)
});

【讨论】:

    猜你喜欢
    • 2020-10-23
    • 2020-12-16
    • 1970-01-01
    • 2017-04-14
    • 2019-09-12
    • 2018-05-24
    • 1970-01-01
    • 1970-01-01
    • 2018-10-03
    相关资源
    最近更新 更多