Pre-request way to avoid a single test from running?

I have 3 tests:

  • Make Decision - Sets idDecision
  • POST TypeA
  • POST Type B

Based upon the set value idDecision, i want to run either “POST TypeA” or “POST TypeB” - never both.

I understand I could use the postman.setNextRequest("") in the "Make Decision"request, but then my decision& routing is very much spaghetti-based:

  • I have to add a new fake request as 4th, to be able to jump to
  • at end of req MakeDecision I have to set a nextReq
  • at end of TypeA I also have to set a nextReq() to fake end-request
    If I would encounter a third POST, I have to rewire a lot.

I was rather hoping to introduce a simple pre-request check, sort of

if (idDecision === someValue) {
   run this request
} else {
   skip this request
}

This way every POST could protect itself from firing off with faulty data & it’s future proof.

Is there a JS way to say in the pre-request JS: “skip current request” ?

2 Likes

Not to my knowledge.
I do a similar thing to what you do.

In the User Service collection, there are several tests ran and each test will add +1 to a failed postman variable.
At the “Teardown - User Service (Password Tests)” I check the “failed” Postman Variable and if it’s value is equal to 0 then I use Postman Set Next Request to tell Postman to end the test there and not go to the next request (The Slack request just messages me on Slack saying “X tests have failed for the Y Test Collection”

  • X being the value of Failed Postman Variable
  • Y being the name of the Test Collection.
if(environment.failed > 0) {
    postman.setNextRequest("Slack - Postman Test Reporter");
} else {
    postman.setNextRequest(null);
}

This means I can have this test run in a Postman Monitor or in our CI and I get notified instantly on Slack for any deviations.
TBH, I probably don’t need to tell Postman to go to the Slack request if the failed value is greater than 0, as by default Postman will go to the next request anyway xD

I should also point out, my Setup/Teardown requests are just hitting the below endpoint which just waits X seconds depending on the numerical value you enter. I obviously enter “0”

https://postman-echo.com/delay/0

2 Likes

There’s an even simpler way to do this.

Wrap the tests for TypeA and TypeB themselves in an if block, so they are directly aware of whether or not they should run. If the test “runs,” but it’s not its turn, it will just return green.