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

编程入门 行业动态 更新时间:2024-10-24 00:19:50
本文介绍了使用定义的函数进行测试时出现 Postman AssertionError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个 Postman 请求,收到了这样的 json 响应:

I have a Postman request getting a json response like so:

{ "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"或类似的内容.所以我试图做一个循环来检查它是哪种类型,然后进行相应的测试:

I'd like to test all the values returned. The problem is that it won't always be in this order; for example, in the future, '64' might be sent first then 'Brawl' then 'Melee' or something along those lines. So I'm trying to make a loop that checks which type it is, then tests accordingly:

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 方法:

And here is the testFunction method:

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

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

This will work when the test passes, but if the test fails I get the following error:

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

每当我执行 'pm.test' 调用'testFunction' 的值不匹配时都是这种情况.

This is the case for anytime I do 'pm.test' that calls 'testFunction' with values that don't match.

我只想让测试失败而不是破坏脚本.

I just want the test to fail rather than break the script.

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

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.

The first one will just fail the test and move on. The second will throw an AssertionError.

推荐答案

两者的区别是

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

With first you are passing a callback function (throws AssertionFailure) while with second you are explicitly calling the function (throws AssertionError)

更多推荐

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

本文发布于:2023-10-29 08:15:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1539183.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   进行测试   定义   AssertionError   Postman

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!