Which syntax is preferable between these options?

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!

2 Likes

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:

https://www.getpostman.com/docs/postman/scripts/postman_sandbox_api_reference

Thanks for the reply and clarification, Kunal, I’ll switch progressively to the pm API.

1 Like

When will Postman stop supporting the legacy entities?

2 Likes

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');
  });

Thanks for that @shamasis :slight_smile:

I have been playing with it already. Quick question actually what is the pm.test equivalent of:
test[“I always fail”] = false;

I believe it’s something like:

pm.test(“I always fail”, function () {
pm.expect(true).to.equal(false);
});

1 Like