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