使用定义的函数进行测试时的Postman AssertionError

时间:2018-07-26 21:09:50

标签: 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'

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

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

核心问题:我不知道这有什么区别:(有效)

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。

1 个答案:

答案 0 :(得分:1)

两者之间的区别是

首先,您传递一个回调函数(引发AssertionFailure) 一秒钟之后,您将显式调用该函数(引发AssertionError)