-1

I am looking at some older Postman test scripts, and since I don't have much experience, I need help understanding this a bit better. The following are in the test scripts

//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);
});

Now, I'd like to put the tests into an if-else statement so rather than a bunch of fails and one pass, it just shows the correct result with the proper schema validated.

Here's how it currently looks Postman Test Result Screenshot

lucas-nguyen-17
  • 5,516
  • 2
  • 9
  • 20

1 Answers1

1

You can do something like this.

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);
    });
   
   ...
}
lucas-nguyen-17
  • 5,516
  • 2
  • 9
  • 20