如何使用if else设置Postman测试?

cwdobuhd  于 2023-04-30  发布在  Postman
关注(0)|答案(1)|浏览(159)

我想在Postman中对这两种情况进行json-schema验证测试-阳性和阴性响应。我的测试是 Postman 。

positiveSchema = {there is a json-schema for positive response}

negativeSchema= {there is a json-schema for negative response}

if (pm.response.to.be.success) {
   pm.test("Positive response", function () {
    pm.response.to.have.jsonSchema(positiveSchema);
});

    } else {

 pm.test("Negative response", function () {
    pm.response.to.have.jsonSchema(negativeSchema);
});
    }

所以,我认为它应该工作:如果响应代码为2XX,则test检查json-schema的有效性。如果响应代码不是2XX,则test检查否定的json-schema验证。
但我有个错误
评估测试脚本时出错:Assert错误:预期响应代码为2XX,但发现500
我的测试有什么问题?
不同的模式- if(pm.response.to.be.success)else if(pm.response.to.错误)。
微型验证器不起作用。但这对我的 Postman 版本来说是可以的。

pm.test('Schema is valid', function () {
    pm.expect(tv4.validate(pm.response, negativeSchema)).to.be.true;
});

如果我不使用“如果其他”-测试工作。但我有一个通过了一个没通过。我不想用这种情况。

kqlmhetl

kqlmhetl1#

问题是pm.response.to.be.success不返回布尔值,它本身就是一个测试,因此当Assert失败时会抛出错误。
要解决这个问题,可以将if语句更改为

if (pm.response.code === 200) {
   ...
}

如果你想要的响应代码属于2xx,那么你可以改变条件是这样的。

if (200 <= pm.response.code &&  pm.response.code <= 299) {
   ...
}

相关问题