Bypass or skip tests for a request

Is there a way to exit a set of tests for a given request early. So in the example below, cut down and simplified, if I don’t get a Person object returned then I’d like to exit at that point. I know I can use a “throw new Error()” but if I do then I can’t see the results of the earlier tests.

Data

{
  "Person": [
    {
      "PersonIdentifiers": {
        "Identifier": [
          {
            "IdentifierType": "PID",
            "IdentifierValue": "123456"
          }
        ]
      },
      "PersonNames": {
        "FirstName": "James"
      }
    }
  ]
}

Script

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

if (pm.response.code === 200) {
	var jsonData = pm.response.json();

	pm.test("Has a Person", function () {
	    pm.expect(jsonData).to.have.property('Person');
	});
// **Exit Here**
	var personObj = jsonData.Person[0];
	
	pm.test("Has an Identifier", function () {
	    pm.expect(personObj.PersonIdentifiers).to.have.property('Identifier');
	});
// **Or Exit Here**
	var identifierObj = personObj.PersonIdentifiers.Identifier[0];

	pm.test("Check IdentifierType", function () {
	    pm.expect(identifierObj.IdentifierType).to.eql("PID");
	}); 
// **Etc.**
	pm.test("Check IdentifierValue", function () {
	    pm.expect(identifierObj.IdentifierValue).to.eql("123456");
	}); 
}
1 Like

Wherever you want to ‘Exit’ you can just use the ‘return’ command.
Mind you, if you use ‘return’ inside a function, it just returns out of the function.

I’ve used it at the script level with no problems.

There might be a better way. If so, wait on for further replies on this thread… I’ll be curious as well.

@TimArmstrong There’s no special way to do so in the app, but javascript gives you a lot of power in the scripts.

A few things you might be able to do are:

  1. You can wrap your tests in an if statement.
  2. Make your tests nested. You can import this collection example in your app and see: https://www.getpostman.com/collections/e6a1c632608449f33da1
  3. Write early bail-out conditions as you already mentioned like using throw or return in the script itself.

In addition to the above response, there’s also another way to directly control the skipping of the complete tests using some control boolean.

You can either use an environment variable or you can perform the computation in the test script whether you want to set a test (or a set of tests) and use that.

For eg:

let skipTest = pm.environment.get('skipTest');

(skipTest ? pm.test.skip : pm.test)("Check status code", () => {
    pm.expect(pm.response.code).to.equal(200);
});

(skipTest ? pm.test.skip : pm.test)("Check status code is not 404", () => {
    pm.expect(pm.response.code).to.not.equal(404);
});
3 Likes

@sivcan, i’m starting withg testing API with script and often use is.a(“object”) or array and often my script breaks the run for exemple when i expect an array, in a next test or a bit late i’m assiging environment variable from the expected response like jsonData[0].myvalue but when the call is returning an error or an object (as managed error message {error:{message:“this is wrong”,code:1234}), the the assignment is generating an error TypeError: Cannot read property ‘0’ of undefined

What would you reocmmand to early avoid thse and be able to continue testing runs, setting the test to fail before getting to the actual/next tests?

Hi @odecroupette, you can go through this post where I’ve answered about safe extraction in Step 1.

1 Like