【问题标题】:Postman AssertionError when testing using a defined function使用定义的函数进行测试时出现 Postman AssertionError
【发布时间】:2018-10-04 23:32:33
【问题描述】:

我有一个 Postman 请求得到一个像这样的 json 响应:

{
    "Rules": [
        {
            "Type": "Melee",
            "Captain": "Falcon",
            "Falco": "Lombardi",
            "Fox": "McCloud",
            "Princess": "Peach",
            "Kirby": null
        },
        {
            "Type": "Brawl",
            "Captain": "Toad",
            "Falco": "The bird",
            "Fox": "Blip",
            "Princess": "Daisy",
            "Kirby": null
        },
        {
            "Type": "64",
            "Captain": "America",
            "Falco": "Dair",
            "Fox": "Shine",
            "Princess": "Float",
            "Kirby": null
        }
    ]
}

我想测试所有返回的值。问题是它并不总是这样。例如,将来可能会先发送“64”,然后发送“Brawl”,然后是“Melee”或类似的东西。所以我正在尝试创建一个循环来检查它是哪种类型,然后进行相应的测试:

for(var i in jsonResponse.Rules)
{
    if(jsonResponse.Rules[i] == "Melee")
        {
            pm.test("Melee Captain is Falcon", testFunction(jsonResponse.Rules[i].Captain, "Falcon");
            pm.test("Melee Falco is Lombardi", testFunction(jsonResponse.Rules[i].Falco, "Lombardi");
            //repeat for fox, princess and kirby
        }
    if(jsonResponse.Rules[i] == "Brawl")
        {
            pm.test("Brawl Captain is Toad", testFunction(jsonResponse.Rules[i].Captain, "Toad");
            //repeat for the rest
        }
    if(jsonResponse.Rules[i] == "64")
        {
             pm.test("64 Captain is America", testFunction(jsonResponse.Rules[i].Captain, "America");                
             //repeat for the rest
        }
}

这里是 testFunction 方法:

function testFunction(value, shouldEqualThis)
{
    pm.expect(value).to.eql(shouldEqualThis);
}

这将在测试通过时起作用,但如果测试失败,我会收到以下错误:

There was an error in evaluating the test script:  
AssertionError: expected 'FalconSpelledWrong' to deeply equal 'Falcon'

每当我使用不匹配的值调用“testFunction”的“pm.test”时都是这种情况。

我只是希望测试失败而不是破坏脚本。

核心问题:我不明白这之间有什么区别:(工作)

pm.test("Melee Captain is Falcon", function() {
    pm.expect(jsonResponseData.Rules[0].Captain).to.eql("FalconSpelledWrong");
})

这个:(不工作)

pm.test("Falcon equals FalconSpelledWrong", stringCompare("Falcon", "FalconSpelledWrong"));

function stringCompare(value, shouldEqualThis)
{
    pm.expect(value).to.eql(shouldEqualThis);
}

第一个将无法通过测试并继续前进。第二个会抛出 AssertionError。

【问题讨论】:

    标签: postman


    【解决方案1】:

    两者的区别是

    首先你要传递一个回调函数(抛出 AssertionFailure) 而第二个你显式调用函数(抛出 AssertionError)

    【讨论】:

      猜你喜欢
      • 2019-10-31
      • 1970-01-01
      • 2019-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多