# Bypass or skip tests for a request

**URL:** <https://community.postman.com/t/bypass-or-skip-tests-for-a-request/2704>\
**Category:** Help Hub\
**Created:** [September 18, 2018, 4:57am UTC](https://community.postman.com/t/bypass-or-skip-tests-for-a-request/2704 "2018-09-18T04:57:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![TimArmstrong](https://avatars.discourse-cdn.com/v4/letter/t/e0b2c6/32.png) [@TimArmstrong](https://community.postman.com/u/TimArmstrong)\
**Post date:** [September 18, 2018, 4:57am UTC](https://community.postman.com/t/bypass-or-skip-tests-for-a-request/2704/1 "2018-09-18T04:57:58Z")

</div>

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**

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

```

**Script**

```javascript
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");
	}); 
}

```

---

<div class="post-metadata">

**Author:** ![jay.krish](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/jay.krish/32/893_2.png) [@jay.krish](https://community.postman.com/u/jay.krish)\
**Post date:** [September 19, 2018, 12:43am UTC](https://community.postman.com/t/bypass-or-skip-tests-for-a-request/2704/2 "2018-09-19T00:43:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![singhsivcan](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/singhsivcan/32/1595_2.png) [@singhsivcan](https://community.postman.com/u/singhsivcan)\
**Post date:** [September 19, 2018, 7:01am UTC](https://community.postman.com/t/bypass-or-skip-tests-for-a-request/2704/3 "2018-09-19T07:01:23Z")

</div>

@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](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.

---

<div class="post-metadata">

**Author:** ![singhsivcan](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/singhsivcan/32/1595_2.png) [@singhsivcan](https://community.postman.com/u/singhsivcan)\
**Post date:** [May 27, 2019, 6:24am UTC](https://community.postman.com/t/bypass-or-skip-tests-for-a-request/2704/4 "2019-05-27T06:24:30Z")

</div>

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:

```javascript
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);
});

```

---

<div class="post-metadata">

**Author:** ![odecroupette](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/odecroupette/32/4102_2.png) [@odecroupette](https://community.postman.com/u/odecroupette)\
**Post date:** [April 1, 2020, 2:08pm UTC](https://community.postman.com/t/bypass-or-skip-tests-for-a-request/2704/5 "2020-04-01T14:08:22Z")

</div>

@singhsivcan, 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?

---

<div class="post-metadata">

**Author:** ![singhsivcan](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/singhsivcan/32/1595_2.png) [@singhsivcan](https://community.postman.com/u/singhsivcan)\
**Post date:** [April 2, 2020, 7:27am UTC](https://community.postman.com/t/bypass-or-skip-tests-for-a-request/2704/6 "2020-04-02T07:27:43Z")

</div>

Hi @odecroupette, you can go through [this post](https://community.postman.com/t/for-loop-for-retrieving-a-attribute-value-from-response-body/4613/2) where I’ve answered about safe extraction in Step 1.

---

<div class="post-metadata">

**Author:** ![singhsivcan](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/singhsivcan/32/1595_2.png) [@singhsivcan](https://community.postman.com/u/singhsivcan)\
**Post date:** [April 2, 2020, 7:35am UTC](https://community.postman.com/t/bypass-or-skip-tests-for-a-request/2704/7 "2020-04-02T07:35:18Z")

</div>

Closed on April 2, 2020, 7:35am UTC


