使用已定义函数进行测试时的Postman AssertionError
我有一个邮递员请求,得到一个json响应,如下所示:使用已定义函数进行测试时的Postman AssertionError,postman,Postman,我有一个邮递员请求,得到一个json响应,如下所示: { "Rules": [ { "Type": "Melee", "Captain": "Falcon", "Falco": "Lombardi", "Fox": "McCloud", "Princess": "Peach", "Kirby": null },
{
"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”可能会先发送,然后是“打架”,然后是“近战”或类似的内容。所以我试着做一个循环,检查它是哪种类型,然后进行相应的测试:
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”时,如果值不匹配,就会出现这种情况
我只是希望测试失败,而不是破坏脚本
核心问题:我不明白这两者之间有什么区别:(工作)
这个:(不起作用)
第一个测试将失败,然后继续。第二个将抛出断言错误。两者之间的区别是 首先,传递回调函数(抛出断言失败) 使用second显式调用函数(抛出断言错误)
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);
}