如何为模式编写Postman if/else语句

nr9pn0ug  于 2022-11-07  发布在  Postman
关注(0)|答案(1)|浏览(222)

我正在查看一些旧的Postman测试脚本,由于我没有太多的经验,我需要帮助来更好地理解这一点。

//schema validation
const schema201PostSuccess = pm.collectionVariables.get("schema201PostSuccess");
const schema400BmcIdFail = pm.collectionVariables.get("schema400BmcIdFail");
const schema401Unauthorized = pm.collectionVariables.get("schema401Unauthorized");
const schema400BadRequest = pm.collectionVariables.get("schema400BadRequest");
const schema404InsufficientPermission = pm.collectionVariables.get("schema404InsufficientPermission");
const schema500InternalServerError = pm.collectionVariables.get("schema500InternalServerError");

pm.test("Wishlist Record Created Schema Validated", function() {
  pm.response.to.have.jsonSchema(schema201PostSuccess);
});

pm.test("Incorrect bmc_id Schema Validated", function() {
  pm.response.to.have.jsonSchema(schema400BmcIdFail);
});

pm.test("Unauthorized Schema Validated", function() {
  pm.response.to.have.jsonSchema(schema401Unauthorized);
});

pm.test("400 Bad Request - Schema Validated", function() {
  pm.response.to.have.jsonSchema(schema400BadRequest);
});

pm.test("404 Insufficient Permission - Schema Validated", function() {
  pm.response.to.have.jsonSchema(schema404InsufficientPermission);
});

pm.test("500 Internal Server Error - Schema Validated", function() {
  pm.response.to.have.jsonSchema(schema500InternalServerError);
});

现在,我想将测试放入一个if-else语句中,这样就不会出现一系列失败和一次通过的情况,它只会显示正确的结果和经过验证的正确模式。
这是它目前的外观Postman Test Result Screenshot

1qczuiv0

1qczuiv01#

你可以这样做。

let status = pm.response.code;

if(status === 201){
    pm.test("Wishlist Record Created Schema Validated", function() {
        pm.response.to.have.jsonSchema(schema201PostSuccess);
    });
} else if (status === 400) {
    pm.test("Incorrect bmc_id Schema Validated", function() {
        pm.response.to.have.jsonSchema(schema400BmcIdFail);
    });

   ...
}

相关问题