How to abort request tests if collection tests fail

My goal is to retry a request if come condition is not met, based on the response. I know how to add retry logic to a specific request test, such that if some condition is not met, the request is retried some number of times until it gives up and runs the else block (usually any pm.test blocks would go in the else block). Something like this:

let maxNumberOfTries = 5;
let retryCodes = [502, 503, 504]

if (!pm.environment.get('collection_request_tries')) {
    pm.environment.set('collection_request_tries', 1);
}

if ((retryCodes.includes(pm.response.code)) && 
   (pm.environment.get('collection_request_tries') < maxNumberOfTries)) {
    let tries = parseInt(pm.environment.get('collection_request_tries'), 10);
    pm.environment.set('collection_request_tries', tries + 1);
    postman.setNextRequest(pm.info.requestId)
} else {
    pm.environment.unset('collection_request_tries');
    pm.test('successful response', function () {
        pm.response.to.have.status(200);
    });
}

What I would like to do is have this retry logic in the collection-level test script such that if the retry condition is met, the request is re-run, and the test script from any folder or request level test is skipped. The challenge that I am seeing is if I add this code to collection-level test:

let maxNumberOfTries = 5;
let retryCodes = [502, 503, 504]

if (!pm.environment.get('collection_request_tries')) {
    pm.environment.set('collection_request_tries', 1);
}

if ((retryCodes.includes(pm.response.code)) && 
   (pm.environment.get('collection_request_tries') < maxNumberOfTries)) {
    let tries = parseInt(pm.environment.get('collection_request_tries'), 10);
    pm.environment.set('collection_request_tries', tries + 1);
    postman.setNextRequest(pm.info.requestId)
} else {
    pm.environment.unset('collection_request_tries');
}

and something like this to the request-level test:

pm.test('successful response', function () {
    pm.response.to.have.status(200);
});

the retry condition is met, the next request is set to the current request id, but the request-level test code is still executed, causing the request-level test to fail. I would like something that looked like this at the collection-level:

let maxNumberOfTries = 5;
let retryCodes = [502, 503, 504]

if (!pm.environment.get('collection_request_tries')) {
    pm.environment.set('collection_request_tries', 1);
}

if ((retryCodes.includes(pm.response.code)) && 
   (pm.environment.get('collection_request_tries') < maxNumberOfTries)) {
    let tries = parseInt(pm.environment.get('collection_request_tries'), 10);
    pm.environment.set('collection_request_tries', tries + 1);
    postman.setNextRequest(pm.info.requestId)
    // some code that aborts further test script execution
    // and skips right to the next request
 } else {
    pm.environment.unset('collection_request_tries');
}

Thanks for reading!!!

Based on a condition set in the collection-level tests, if you want the request to run, but not the request-level tests - maybe you can try pm.test.skip in the request-level test script. See code sample.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.