How to set environment through script?


Just wondering if there is a way we can select an environment through script without any manual intervention?

Typically a code snippet in Pre-requisite Script would select the environment prior to running a collection.

Yes you can do that. In the Pre-request Script tab of the relevant artifact use the following script:

pm.collectionVariables.set('environment', '<your-value>');


All that does is set a collectionVariable called “Environment”.

The original post asked if it possible to set the environment using a script.



I can’t understand why you would want to do this. You would then have to update your script if you wanted to change your environment? You would be better off just using collectionVariables instead.

Yes, I see the downside of hard coding it to only run against specific environment and rework.

Similarly, defining the variables at collection level would have me separate collection for dev and systest environments. That would also complicate things. Unless im missing something.

Regarding collection variables, you don’t need separate collection targeting different environments. I am using only one set of collection variables, have organized my folders for different environments and in the Pre Request Script tab of each folder, setting up proper values is working right for me.

This is where Postman could do with future enhancements.

I would prefer to see test suite → test case → test scripts rather than collections and folders. I’m sure they could add this in addition to the collections and folders, not as a direct replacement.

You should have the ability to tag the test cases (not the script) with the environment they are allow to run against. A test case may have multiple steps\scripts so this seems to be the appropriate place for any tags.

If the environment is set to PROD, but the test case isn’t tagged with PROD, then it won’t run.

This way, it should be one set of test data, and one set of tests to maintain across the environments.

At the moment, you have to jumps through hoops to apply any tagging to your tests. Which is usually basic functionality in an automated test suite.

Thanks. I echo your thoughts as well.

Thanks for your thoughts.