postman-bdd icon indicating copy to clipboard operation
postman-bdd copied to clipboard

How to stop the test case if the test fails.

Open LennonRuangjaroon opened this issue 7 years ago • 5 comments

When some test failed.I would like to terminate the script. Can I terminate next test?

` eval(globals.postmanBDD);

describe('Get customer info', () => {

it('should return a 200 response', () => {
    response.should.have.status(200);
    
    // in case of status, not 200. I would like to terminate the test.
});

// There isn't run this.
it('test 2', () => {
});

}); `

LennonRuangjaroon avatar Sep 04 '17 13:09 LennonRuangjaroon

There's not currently an easy way to do that, but I like the idea. I could add something like an abort() function that would skip any remaining tests and stop the collection.

JamesMessinger avatar Sep 04 '17 16:09 JamesMessinger

Great request. I've been thinking of adding this as a feature request myself.

zac11 avatar Sep 05 '17 17:09 zac11

Have had to face down this problem and have found the following hack:

  1. Create a do-nothing script (Say simply GET google) that executes before all others with the following in pre-request: pm.environment.set("post_exec_script", "MyPostExecScript");

  2. Create another do-nothing script that is the last file in your collection/folder

  3. In your test, have the following function at the top:

function testWrapping(message, testFunc) { var testPassing = false;

pm.test(message, function () {
    var innerTestRes;
    try
    {
       innerTestRes = testFunc();
       testPassing = typeof(innerTestRes) === "undefined" ? true : innerTestRes;
    }
    catch(ex)
    {
        throw ex;
    }
    return innerTestRes;
});

if(!testPassing)
    postman.setNextRequest(pm.environment.get("post_exec_script"))

return testPassing;

}

  1. Call it like this and it will drop out of execution of any more tests & subsequent requests:

if(!testWrapping("response is ok", function () { pm.response.to.have.status(200); }) ) return;

dotdashnotdot avatar Feb 27 '18 11:02 dotdashnotdot

FYI - If you pass null to the postman.setNextRequest() function, it will automatically terminate the collection run. So there's no need to create a do-nothing request just to pass to setNextRequest()

JamesMessinger avatar Feb 27 '18 12:02 JamesMessinger

Yup, a null is probably better if you want to just fail a whole collection. I only need it the fail on folder scope and to attempt the next group of requests though so included this approach :)

dotdashnotdot avatar Feb 27 '18 12:02 dotdashnotdot