Search code examples
testingautomated-testspostman

Add if else verification in POSTMAN test script


I am creating a test case where I am expecting to pass the test if response code is 200, and if it's not 200 then check for response code as 500 and "error_msg": "token失效,请重新登录". If this is true then test is passed, but if any other error msg is generated test fails.

{
    "code": "lid5000294001",
    "status": "error",
    "data": {
        "error_msg": "token失效,请重新登录"
    },
    "timestamp": "2023-05-03 14:52:05",
    "traceId": "c92e2e2f679a4cd893bb6980134247ef"
}

Please suggest what shall be added in the test script? Currently I am only having

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

Solution

  • The code would be:

    if (pm.response.code === 500) {
        pm.test("Status code is 500", function () {
            const res = pm.response.json();
            pm.expect(res.data.error_msg).eql("token失效,请重新登录")
        });
    
    } else {
        pm.test("Status code is 200", function () {
          pm.response.to.have.status(200);
        });
    }