For example, is there a way to only execute test script code if it’s being run by the runner and not by pressing send?
Hi @robmcec!
The pm
API doesn’t have anything to check if it’s being executed as part of a collection run, but you can do this yourself.
- Add a Request above the one that includes your tests. It can go to a dummy URL, like
https://postman-echo.com/get
. In the Tests tab, set a variable to indicate you’re in a collection run.
pm.variables.set('isCollectionRun', 'true');
- Now, in the Tests tab of the Request for which you want to conditionally execute tests, add some logic around this variable.
const isCollectionRun = pm.variables.get('isCollectionRun') === 'true';
if (isCollectionRun) {
pm.test('status code is 200', () => {
pm.response.to.have.status(200);
});
}
Now, when you run just the individual request, the test shouldn’t execute. When you run it as part of a collection run, the initializing request will execute first, setting the variable, the condition will past, and the test will run.
Hope this helps.
Best,
Kevin
1 Like