JamesMessinger / postman-bdd

A BDD test framework for Postman and Newman
http://www.getpostman.com
MIT License
140 stars 29 forks source link

How to stop the test case if the test fails. #17

Open LennonRuangjaroon opened 7 years ago

LennonRuangjaroon commented 7 years ago

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', () => {
});

}); `

JamesMessinger commented 7 years ago

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.

zac11 commented 7 years ago

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

dotdashnotdot commented 6 years ago

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;

}

4) 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;

JamesMessinger commented 6 years ago

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()

dotdashnotdot commented 6 years ago

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 :)