3

Is it possible to stop a test running in the pre-request script but not stop the whole test collection running?

I have seen various posts mentioning these ways:

  • postman.setNextRequest(null);
  • throw new Error("Error");

These will stop the test from completing but also seems to stop the whole test collection run which I don't want.

Update

Pre-request script:

var test = 'Wait for create transaction request to complete';

var numRetriesRemaining = postman.getEnvironmentVariable("numRetriesUntilCompletionExpected");

if (numRetriesRemaining == -1) {
    // stop the request from being sent and stop the "Tests" part from running 
    // but also don't stop other tests in the collection
}
Chris
  • 3,113
  • 5
  • 24
  • 46

1 Answers1

0

If I misunderstood you, then I'll update this answer.


Writing a simple return in the test script can stop the further execution of the test script.

You can do the following in the pre-request script:

pm.test('This test should run', function () {});

pm.test('This test should also run', function () {});

return;

pm.test('This test should not run', function () {});

You can add a return inside the test also to stop the execution of the test. For eg:

pm.test('This test should execute only one case and not the last one', function () {
    pm.expect(1).to.equal(1); // This will execute

    return;

    pm.expect(2).to.equal(1); // This will not execute
});
Sivcan Singh
  • 1,775
  • 11
  • 15
  • 1
    hi Sivcan, thanks for your answer. You are right, `return` stops the pre-request script from running but I also need to stop the "Test" part of the request from running too - any ideas? – Chris Apr 02 '19 at 20:06
  • You can add `return` in the `Test` section of the request too, that also works. If there's still something that I misunderstood, can you explain your problem in more detail then? – Sivcan Singh Apr 03 '19 at 11:24
  • Please see the "Update" in my question. Thanks. – Chris Apr 03 '19 at 15:37