Conditional test

I am a novice to both javascript and Postman I have written a conditional class for test execution of tests but it’s not giving expected results. Can some help me here

pm.test("Check the status code",function(){
    if(pm.response.status !== 200)
    {
        postman.setNextRequest("GetItem")
    }
    else
    {
        console.log("Check authentication")
    }
});

I am expecting to this to fail but it’s not

Hey @vinai.bw,

Welcome to the community! :wave:

In order for it to fail, it would need a pm.expect() statement to fail against. If it doesn’t have one, it will always ‘Pass’.

For example:

pm.test("Response time is less than 200ms", function () {
    pm.expect(pm.response.responseTime).to.be.below(200);
});  

If the response time was above 200ms, the test would fail.


I’m not really sure of the full context here but you could try something like this:

pm.test("Check the status code", function(){
    if(pm.response.code !== 200)
    {
        pm.expect.fail("Status Code is not 200")
        postman.setNextRequest("GetItem")
    }
    else
    {
        pm.response.to.have.status(200);
        console.log("Check authentication")
    }
});

More information about tests on our learning site:

https://learning.postman.com/docs/postman/scripts/test-scripts/

1 Like