Is there a way to track how many times a test script has run?

We have a test script for an endpoint that says:

var sl = postman.getEnvironmentVariable(‘shouldLoop’);
if (sl !== ‘yes’) {
postman.setNextRequest(null);
return;
}

else
// run some code

…but I’d like to change the first if-statement to say:

“If sl !== ‘yes’ OR [script has already been run X times])”

…because this has the potential to run forever without that. Is there a way to track that?

@jared531

Is your “loop” looping back to a previous “request” ? like below …

var sl = postman.getEnvironmentVariable(‘shouldLoop’);
if (sl !== ‘yes’) {
    postman.setNextRequest(null);
return;
}
else {
    postman.setNextRequest("A Previous Request");
}

Hi @dhoyt, there’s a lot more code than that but that is essentially what is happening yes. There’s a condition further down that continues to return false and every time it does it sets the request to a previous one. Would be nice to be able to tell that condition to only try four times via four script runs before it just marks the test as false.

Actually I think I’ll just set another environment variable to integer and increment it to keep track of this. Should work I think.

@jared531

In the previous request you could do something like this to increment the count each time the request is run and then in the next request you could pull the environment variable and test it.

if ( typeof(pm.environment.get('count')) === 'undefined' ) {
    pm.environment.set('count', 1);
}
else {
    new_count = parseInt(pm.environment.get('count')) + 1;
    pm.environment.set('count', new_count);
}

Yep I’ll do something like that. Thanks for the help!

1 Like