Postman Test Scripts
Don't Allow a Collection Run to Continue if a Required Resources is Unusable or Does Not Exist
The postman.setNextRequest() method can be used to control the flow to a test other than the next test in the list (see this blog on Postman Conditional Workflows for more). But even better, it can be used to stop a collection run.
For example, assume a data domain is created at the start of a test run and is required for the following tests in a Postman collection run, this code block in the request's test script will stop the run if the domain is not created:
Simple Example
tests["Status code is 201/202"] = responseCode.code === 201 || responseCode.code === 202; if (responseCode.code != 201 && responseCode.code != 202) { tests["Scrap this run: Domain not created"] = true === false; postman.setNextRequest(null); }
More Complex Example
tests["Status code is 201/202"] = responseCode.code === 201 || responseCode.code === 202; if (responseCode.code == 201 || responseCode.code == 202) { // // #1: do stuff like make sure there is a response body and // retrieve env vars to be used by other tests in this // collection // } else { // // #2: Bail on the collection run // tests["Scrap this run: Domain not created"] = true === false; postman.setNextRequest(null); } // // #3: Could add more extensive stuff beyond #1 here //