【问题标题】:How to check if the response includes specified value and if so - end test in Postman如何检查响应是否包含指定值以及是否包含 - 在 Postman 中结束测试
【发布时间】:2023-01-09 23:42:57
【问题描述】:

我正在学习 Postman 测试脚本,但我坚持做一个练习。我需要检查响应是否包含指定值(数组中的行星之一称为 Tatooine)。身体反应:

   "results": [
        {
            "name": "Tatooine",
            "rotation_period": "23",
            "orbital_period": "304",
            "diameter": "10465",
        {
            "name": "Alderaan",
            "rotation_period": "24",
            "orbital_period": "364",
            "diameter": "12500",
        },

我创建了这个脚本:

const jsonData = pm.response.json();

pm.test("Your test name", function () {
    for (let i = 0; i <= jsonData.results.length; i++) {
        pm.expect(jsonData.results[i].name).to.include("Tatooine")};

});

但我不知道如何在找到搜索值后跳出循环并将测试标记为“已通过”。

【问题讨论】:

    标签: json testing postman


    【解决方案1】:

    我假设您想验证至少有一个名称Tatooine

    第 1 步:获取所有名称

    const jsonData = pm.response.json();
    let names = _.map(jsonData.results, "name");
    

    第 2 步:验证数组 names 包含 Tatooine

    pm.test("Your test name", function () { 
      pm.expect(names).to.include("Tatooine")
    });
    

    【讨论】:

      猜你喜欢
      • 2018-02-13
      • 2017-05-16
      • 2021-04-08
      • 1970-01-01
      • 2020-05-24
      • 1970-01-01
      • 2019-01-04
      • 2014-10-08
      • 2011-04-15
      相关资源
      最近更新 更多