Rest – How to read environment variables in Postman tests

postmanresttesting

I'm using the packaged app version of Postman to write tests against my Rest API. I'm trying to manage state between consecutive tests. To faciliate this, the Postman object exposed to the Javascript test runtime has methods for setting variables, but none for reading.

postman.setEnvironmentVariable("key", value );

Now, I can read this value in the next call via the {{key}} structure that sucks values in from the current environment. BUT, this doesn't work in the tests; it only works in the request building stuff.

So, is there away to read this stuff from the tests?

Best Answer

According to the docs here you can use

environment["foo"] OR environment.foo
globals["bar"] OR globals.bar

to access them.

ie;

postman.setEnvironmentVariable("foo", "bar");

tests["environment var foo = bar"] = environment.foo === "bar";

postman.setGlobalVariable("foobar", "1");

tests["global var foobar = true"] = globals.foobar == true;

postman.setGlobalVariable("bar", "0");

tests["global var bar = false"] = globals.bar == false;
Related Topic