Postman:是否可以根据先决条件脚本中检测到的条件阻止执行 postman 调用?先决条件、可以根据、脚本、检测到

2023-09-07 10:49:08 作者:阳光少年

我在第一次调用中使用预请求脚本为整个脚本动态生成必要的环境变量.我还希望用户在通过收集运行程序运行时收到这些故障的通知,而无需查看控制台.是否可以在测试或其他替代方法中生成信息,以便在收集运行器结果中明确显示失败?

I am using the pre-request script in the first call to dynamically generate essential environment variables for the entire script. I also want the users to be notified of those failures when running via collection runner without having to look up to the console. Is it possible to generate information in tests or some other alternative so failures are explicit in the collection runner results?

例如如果环境中没有提供 ip,则运行登录调用是没有意义的.所以我想写一个先决条件脚本:

e.g. if the ip has not been provided in the environment, it does not make sense to run the login call. So i would like to write in a pre-requisite script:

if (!environment['IP']) {
    //do not execute any further and do not send the REST call
}

我尝试使用:

if (!environment["xyz"]) {
    tests["condtion1"]=false
}

但它给出了错误:

There was an error in evaluating pre-requisite script: tests is not defined

是否有任何解决方法 - 我不想将此代码移动到测试选项卡,因为我不想在不相关的环境条件下使代码混乱.

Is there any workaround - I don't want to move this code to the tests tab as I don't want to clutter the code there with unrelated environment conditioning.

推荐答案

投掷就可以了.(更新了来自@Joe White 的优秀提示)

A throw works just fine. (Updated with excellent tip from @Joe White)

if (!environment['X']) {
    throw new Error('No "X" set')
}

这会阻止 REST 调用通过.但在收集运行器模式下,它会停止整个测试套件.

This prevents the REST call from going through. But in the collection runner mode it stops the entire test suite.

但是当与 newman collection runner 结合使用时,它工作得很好.

But when coupled with newman collection runner it works just fine.