0

I've defined several collection tests that fire after each individual test in collection. See below

pm.test("Status code is not of error type", function() {
    pm.expect(pm.response.code).to.not.eql(500);
});

pm.test("Content-Type is present", function () {
    pm.response.to.have.header("Content-Type");
});

pm.test("Response must be valid json", function () {
     pm.response.to.be.withBody;
     pm.response.to.be.json;
});

I'd like to extend them further. Ideally, I'd like to run different tests based on the method type they were sent with. For instance, I'd like to test each DELETE request for the following.

pm.expect(pm.response.code).to.be.oneOf([204, 409])

Is it possible to define this at a collection level? Or do I need to paste this line into each delete request I have?

Dan
  • 473
  • 3
  • 13

1 Answers1

1

To promote visibility it'd probably be best to put such a test at the request level, or group similar requests in a folder and apply the tests at a folder level. The purpose of folder/collection level tests is to apply the same tests across a broad spectrum.

If you did really want to do it, you can wrap your test in an if condition:

if (pm.request.method === 'DELETE') {
  pm.expect(pm.response.code).to.be.oneOf([204, 409]);
}
Matt
  • 720
  • 8
  • 15
  • Sorry for late response. Thank you kindly, I was not aware that you can define tests also at a folder level. That's perfect. – Dan Apr 05 '19 at 15:34