Run specific requests based on environment variable

My question:

In a single collection, I want to run certain requests OR skip requests based on the environment variable. Is there a way to set conditions inside the requests that they will only be executed when environment variable is met?

For example

Req1 for client1
Req2 for client1 and client2
Req3 for client2
Req4 for client1 and client3

Environment variable:
Client=client1

Expected: Only Req1, Req2, and Req4 should be executed.

I know that I can create different collections for different clients, but that would be a hassle. In our case, clients sometimes only have 1 or 2 requests that cannot be executed out of 50 requests. And when we do our changes, all clients will be affected, thus, maintaining only 1 collection would make sense for us.

I tried setNextRequest(), however, that already executes the request and just skips the checking. I need it to really not execute certain requests.

Thank you!

I’ve already tried: I tried setNextRequest(), however, that already executes the request and just skips the checking. I need it to really not execute certain requests.

Hi @flight-participant11

You could put your setNextRequest() call into an IF statement.

for example:

if (pm.environment.get("client_id") === "your_client_id") {
    // Your logic when client ID matches
    postman.setNextRequest("Next Request Name");
} else {
    // Your logic when client ID doesn't match
    postman.setNextRequest("Another Request Name");
}
1 Like

This is functionality that comes out of the box in most automation tools.

The ability to tag requests\tests and to then target the tests in the runners with or without those tags.

You may have a collection with a full set of tests, but only want the read only ones run against production but don’t want to have separate collections or folders in order to do this.

Not sure setNextRequest would work as the first request would always run and if that is one of the requests you don’t want to run, then you are out of luck. Also working out which request should run next would be fun.

I suspect this would be an enhancement feature for the runner to allow some conditional logic.