How do I write test for same request with different body?

Hi @leen, you can write tests conditionally.

This is just an example to do so:

let resp = {};

// This is because if you try to do `JSON.parse` on a response
// which is not a JSON then it'll throw an error
try {
    resp = pm.response.json();
}
catch (e) {}

pm.test(`response code should be ${pm.response.code} based on email availability`, function () {
  if (_.get(resp, 'email')) { // Using lodash which is built-in postman
    pm.response.to.have.status(200);
  }
  else {
    pm.response.to.have.status(404);
  }
});

There can be multiple ways to do things like these, based on what you’re checking.
But yes, you can use the entire power of javascript to do things like these.

I also used lodash which is built-into postman.
There are multiple other libraries that are built-in with postman and you can use them in the pre-request / test scripts.

3 Likes