【问题标题】:How to verify in postman regardless of the number of results if the result returns如果结果返回,无论结果数量如何,如何在邮递员中进行验证
【发布时间】:2019-03-30 17:35:00
【问题描述】:

无论响应中的所有数据返回 id、firstname、lastname 等结果的数量如何,如何在 postman 中进行验证

下面是响应的样子:

[
    {
        "id": 1,
        "first_name": "Sebastian",
        "last_name": "Eschweiler",
        "email": "sebastian@codingthesmartway.com"
    },
    {
        "first_name": "Sebastian",
        "last_name": "Eschweiler",
        "email": "sebastian@codingthesmartway.com",
        "id": 4
    },
    {
        "id": 5,
        "first_name": "Sebastian",
        "last_name": "Eschweiler",
        "email": "sebastian@codingthesmartway.com"
    },
    {
        "first_name": "Sebastian",
        "last_name": "Eschweiler",
        "email": "sebastian@codingthesmartway.com",
        "id": 8
    },
    {
        "id": 9,
        "first_name": "Sebastian",
        "last_name": "Eschweiler",
        "email": "sebastian@codingthesmartway.com"
    },
    {
        "first_name": "Sebastian",
        "last_name": "Eschweiler",
        "email": "sebastian@codingthesmartway.com",
        "id": 12
    }
]

我想验证两件事:

1) 响应返回 id、first_name、last_name、email

2) 无论只有一个结果还是 100,所有 first_name 都等于“Sebastian”

这是我尝试过的,但它只适用于一个结果:

const jsonData = pm.response.json();

pm.test('Has data', function() {
  pm.expect(jsonData).to.have.property('first_name');
  pm.expect(jsonData).to.have.property('last_name');
  pm.expect(jsonData).to.have.property('email');
  pm.expect(jsonData).to.have.property('id');

});

【问题讨论】:

  • 您是否尝试过检查返回的 JSON 是 JSON 对象还是 JSON 数组?如果它是一个数组,您可能需要将您的 has 数据检查放入 forEach/for 循环中。

标签: javascript node.js postman postman-collection-runner


【解决方案1】:

你可以试试这个:

pm.test("Has data",() => {
    _.each(pm.response.json(), (item) => {
      pm.expect(item.first_name).to.eql("Sebastian")
      pm.expect(item).to.have.property('first_name')
      pm.expect(item).to.have.property('last_name')
      pm.expect(item).to.have.property('email')
      pm.expect(item).to.have.property('id')
    })
})

这将根据您提供的数据集进行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-15
    • 2011-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-06
    相关资源
    最近更新 更多