Add the ability to ignore/disable a block of tests at the collection level

In my collection, for all the requests I have 3 scenarios:

  1. when the response code is 200 and the response body is not empty.
  2. when the response code is 200 and the response body is empty.
  3. When response code is other than 200

For 2nd and 3rd, I have added functions at the collection level and that’s working fine.

For the 1st, I want to add a function at the collection level, basically, the function should check if the response code is not 200 then don’t executed the block of scripts written at the request level.

The purpose of adding this conditional check (responseCode.code === 200 && jsonData.data.length > 0) at the collection level because I have more than 500 requests in my collection and I don’t want to go and add this piece of code in all 500 requests.

This check is important in this case when the response body is empty and the response code is 200 then it’s throwing "Cannot read property '0' of undefined" because the script is trying to validate some elements which are not present in the response body.

I tried so many things but it didn’t work out.

Please suggest how we can add a function at the collection level to check if the response code !=200 then skip the whole block of test scripts written at the request level

Hey @babalisingh90

Welcome to the community! :rocket:

Would you be able to share the full code snippet of what you have in place / what you’re tried so far please?

If you just wanted something quick that would stop the script execution for a certain condition, you could something like this:

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

pm.test("test", () => {
    pm.expect(1).to.equal(1)
})

So if the response code in not 200, it will stop the execution of the test below it. This is just a basic and very simple example and doesn’t account for any context based information.

Thanks, @dannydainton for the quick response.

I tried the same thing and its working fine when I am adding in the test tab. Please refer the below code snippet (in Tests tab):

if (pm.response.code !== 200) 
    {
      return
    }
    else if (pm.response.code === 200 && jsonData.data.length === 0)
    {
        return
    }

Now I want to wrap this code in a function that can be used in other requests. please refer the code snippet.
Pre-requisite tab:

var testingcase =  () =>
{
    if (pm.response.code !== 200) 
    {
      return
    }
    else if (pm.response.code === 200 && jsonData.data.length === 0)
    {
        return
    }
}
pm.environment.set("testingcase",testingcase.toString());

Tests:

 eval(pm.environment.get("testingcase"))();

Error getting in this case: There was an error in evaluating the test script: TypeError: Cannot read property ‘email’ of undefined

It seems some issue with the function, that’s why its not ignoring the tests.

So, my goal is to create a function that will validate the response code and length of the response body and if the condition is not met then it should skip the execution of the tests below it.

I want to check for all my requests so I am trying to create a function that can be accessible for all the requests.

@dannydainton Any luck on this?

Umm…what am I supposed to be doing for this :smiley:

Can you share a collection with the code you’re trying and an example of the response body so we can try and replicate this locally.