Does postman know how it's test scripts are being executed?

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.

  1. 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');
  1. 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