【问题标题】:In postman tests, how can i assert the response values with one of multiple values when the input parameter can be empty?在邮递员测试中,当输入参数可以为空时,如何使用多个值之一断言响应值?
【发布时间】:2021-01-09 07:09:04
【问题描述】:

我有一个名为 IsFruit 的输入参数,它可以是 0 或 1。如果值为 0,则响应应返回 FruitsYN 值为 N 的水果。类似地,如果值为 1,FruitsYN 应为 Y。如果此参数有没有值,响应可以有 FruitsYN Y 或 N。这是我编写的代码,但是虽然有些情况是通过,但有些情况是失败的。当输入中的值为空时,我打印了 IsFruit。看起来像∅

var requestData = JSON.parse(request.data);
var responseData = JSON.parse(responseBody);
var IsFruit=requestData.IsFruit;// IsFruit can be either 0,1 or empty
pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
    pm.test("Check if Fruits found with this search criteria", function() {
        pm.expect(responseData.Message).to.not.eql("No Fruits found with this search criteria");

        var list = (responseData.Fruits).length;
        //console.log(list);
        var a = [];
        
        for (var i = 0; i < list; i++) {
            var counter = responseData.Fruits[i];
            FruitsYN = counter.FruitsYN
            //console.log(FruitsYN);
            a.push(FruitsYN)
            pm.test("Check whether Fruit values in the Fruits returned is accurate based on fruit filter in the request", function() {

                if (IsFruit == 0) {

                    pm.expect(FruitsYN).to.eql("N")

                }
                if (IsFruit == 1) {
                    pm.expect(FruitsYN).to.eql("Y")
                }

                if (IsFruit =="") {
                    pm.expect(FruitsYN).to.be.oneOf(["Y", "N"]);
                }
            });
        }
    });
});

【问题讨论】:

  • 示例输入:{ "Xyz":"blah blah", "Abc":"111", "IsFlip":"", "Asd":"4" } 请注意,如果输入为空,我的代码将失败

标签: javascript json postman postman-collection-runner postman-testcase


【解决方案1】:

看起来以大写字符开头的变量名在 Postman 中不起作用。

以下代码对我有用:

let requestData = {"IsFruit":""};
let isFruit=requestData.IsFruit;// IsFruit can be either 0,1 or empty

if (isFruit =="") {
    console.log("Empty string");
}

而如果我使用 IsFruit 则它不起作用

【讨论】:

  • 我认为这不是问题所在。因为我得到了 1 和 0 场景的正确测试结果。仅当值为空时,测试才起作用。我也尝试过 isFruit ,但它仍然显示错误。我相信这是因为我们在输入为空时使用的 if 条件。当 isFruit 的值为空时,我在控制台中得到一个 ∅ 作为 isFruit 的值。
  • 仅供参考,这是我得到的错误,根据请求中的水果过滤器检查返回的水果中的水果值是否准确 | AssertionError: 预期 'Y' 深度等于 'N' 。然而,在此之前的测试用例通过了,可能是因为它们的 FruitYN 值为 N。只要 FruitYN 为 Y,测试就会失败。可能是因为循环吗?
  • 请求失败:{ "Name":"blah blah", "Color":"Red", "IsFlip":"", "Price":"4" } 失败响应:{ “FruitYN”:“Y”,“Fruitprice”:“658”,“FruitType”:“Full”,“Taste”:“Inside”,“Origin”:“Unknown”,“Seeds”:“Unknown”,“Sweetness” “:“其他”,“卡路里”:“未知”,“Id”:“55640777”,“重量”:0.10,“密集”:“假”,“计数”:1,“ImageCount”:28 } 测试仅当值为 Y 时,案例才会失败
  • 您的请求不包含IsFruit
  • 我修好了。 :) 。我刚刚添加了一个新的 if 条件。显然问题是因为空值。所以我像这样处理它if (isFruit==""){ isFruit="empty" 然后我稍后在条件检查中使用它if (isFruit=="empty") {pm.expect(FruitYN).to.be.oneOf(["Y","N"]);} 谢谢:)
【解决方案2】:

我修好了。 :) 。我刚刚添加了一个新的 if 条件。显然问题是因为空值。所以我这样处理 if (isFruit == "") { isFruit = "empty" 然后我稍后在条件检查中使用它 if (isFruit == "empty") { pm.expect(FruitYN).to.be.oneOf(["Y", "N"]); }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-10
    • 2018-06-11
    • 2020-09-12
    • 2023-02-14
    相关资源
    最近更新 更多