How can I stop test immediately or set timeout while testing

Hello! I have one postman Pre-request Script Test. I send many async requests and expected to get response. But how can I stop it while testing if some one request was failed? Or can I set timeout and continue the Test after 1-2 sec ?

Can I write some Code like this ?:

pm.expect(res.code).to.be.oneOf([200, 400, 500])
else { pm.StopTest(); }

or come like this :

pm.expect(res.code).to.be.oneOf([200, 400, 500])
else { pm.Wait(1000); }

1 Like

So we have the need sometimes to wait for processing between calls in some of our tests or in the case of a failed call, retry. In those cases we do something like this:

if (responseCode.code === 200) {
        // Delay for 60 Seconds (set in milliseconds)
        setTimeout(function(){}, 60000);
}
else
{
        // Retry the call again
        postman.setNextRequest("CreateData");
}

So basically this would be housed in my Tests tab but it simply checks the response code, if its what we want then wait 60 seconds before proceeding to the next call.
Otherwise retry the call because for some reason it was not successful. There is also some logic you can add in here to retry X times and then stop the run or do some other action based on this outcome, because there is a chance you could get into an endless loop. For this example I kept it simple.

Hope this helps.

1 Like