javascriptweb-servicespostman

How to test for multiple acceptable status codes in Postman?


I have an endpoint that can return a status code 200 or 400 depending the time of day.
How would I go about creating a test that can check for either of them?

In the test snippets, there is a test that would assert one status code:

pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});

But, this is only for one status code.
How could it be adapted to assert multiple acceptable status code?


Solution

  • pm.test('Check status code', () => {
        pm.expect(pm.response.code).to.be.oneOf([200, 400])
    })
    

    That’s a quick example of a test that would check to see if the status code was either 200 or 400.