I’m still relatively new to Postman (I really started using it in July) and I’ve already come across different ways to access and set data. Maybe it’s due to recent changes and improvements to Postman but I’d like to have confirmation of which direction I should go, like with the following examples:
Should I use pm.response.code rather than responseCode.code?
Should I use pm.environment.get(“myVariable”) rather than environment.myVariable?
Should I use pm.environment.set(“myVariable”, “string”) rather than postman.setEnvironmentVariable(“myVariable”, “string”)?
Should I use pm.test(“Status code is 200”, function () { pm.response.to.have.status(200);}); rather than tests[“Status code is 200”] = responseCode.code === 200; ?
Currently, all of these work but going forward, what’s best? Does it matter? Thanks for your help!
Hey Josselin, we recommend using the pm.* family of functions, better known as the pm API. The pm API is meant to grant users more control and features within their request scripts. In addition, this new pm API will soon replace the legacy entities like responseCode, environment, tests, and so on. More details on the same can be found here:
Not anytime soon @jeronimus_tui! We will mostly keep it as-is for a long long long time and develop all cool new stuff around the pm.* namespace. You should try it, if you haven’t. Especially the new way to write test assertions. They look clean, manageable and displays more verbose error when tests fail.
pm.test('response should not return an error', function () {
pm.response.to.not.be.error;
pm.response.to.not.have.jsonBody('error');
});