gpt4 book ai didi

testing - 如何在 Postman 的新 pm.* API 中使用条件语句?

转载 作者:行者123 更新时间:2023-11-28 19:55:02 24 4
gpt4 key购买 nike

我有一个关于如何使用带有条件语句的新 pm.* API 的问题。请看下面的示例代码

if(pm.test("Status code is 200", function({
pm.expect(pm.response.code).to.equal(300);})){

var token = pm.response.headers.get("Authorization");
pm.environment.set("JWT Token", token.substr(7));

pm.test("Response body is empty ", function () {
pm.expect(pm.response.text().length).to.equal(0);
});
}

console.log(pm.test("Status code is 200", function() {pm.expect(pm.response.code).to.equal(300)}));

因为我可能想要执行某些测试,例如只有当返回 200 时我才想使用 if。但是,当我故意将相等值更改为 300 以检查这是否有效时,即使第一个测试失败,两个测试都会运行并设置变量。

console.log 返回一个空对象而不是断言的 true/false。如果我使用类似的东西

console.log(pm.expect(pm.response.code).to.equal(300));

我会收到一条错误消息:

There was an error in evaluating the test script: AssertionError: expected 200 to equal 300

有人知道怎么解决吗?

干杯

最佳答案

我也在 Postman 的 github 上问过这个问题,我得到了帮助。显然 pm.test 不会返回测试结果,因为您可以在一个 pm.test 中有多个断言,并且这些断言是异步执行的。

以下代码按预期工作:

pm.test("Status code is 200", function() {
pm.expect(pm.response.code).to.equal(200);
});

(pm.response.code===200 ? pm.test : pm.test.skip)("Response body is empty", function () {
pm.expect(pm.response.text().length).to.equal(0);

var token = pm.response.headers.get("Authorization");
pm.environment.set("JWT Token", token.substr(7));
});

关于testing - 如何在 Postman 的新 pm.* API 中使用条件语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46929974/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com