Search code examples
postmannewman

Postman newman handle status codes


I have a question if it is possible to have more than one status code check in test. Case is 201 when user is created and 409 when user already exist when i re-run test cases with same batch of users. 201 is handled but 409 not. a

 var data = pm.response.json()
 pm.test("Status test", function () {
  return  pm.response.to.have.status(200) || pm.response.to.have.status(409);
     
});


There was an error in evaluating the test script:  JSONError: Unexpected token 'U' at 1:1 User already Exists ^

EDIT:

For 200 i am getting user object i cannot post this object here

for 409 string User already Exists


Solution

  • A more elegant way to test for status 200 or 409 would be:

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

    The error occurs when status is 409, because then the request doesn't return JSON, but a string. I would put something like

    if (pm.response.code == 200) {
        var data = pm.response.json()
        // your code
    }
    

    But I strongly advise you, not to do this! In terms of testing, you want one test to only check for one thing. Otherwise this most probably will lead to misunderstandings and increased maintenance effort.

    I would propose to create one request to create users, with according tests. And another one, that tries to recreate the users, again with according tests.