Skip to content
This repository has been archived by the owner on Jul 31, 2018. It is now read-only.

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

Open
LennonRuangjaroon opened this issue Sep 4, 2017 · 5 comments
Open

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

LennonRuangjaroon opened this issue Sep 4, 2017 · 5 comments

Comments

@LennonRuangjaroon
Copy link

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 LennonRuangjaroon changed the title How to stopping test case when finding failed test How to stopping test case. Sep 4, 2017
@LennonRuangjaroon LennonRuangjaroon changed the title How to stopping test case. How to stopping test case if test fails. Sep 4, 2017
@LennonRuangjaroon LennonRuangjaroon changed the title How to stopping test case if test fails. How to stop the test case if the test fails. Sep 4, 2017
@JamesMessinger
Copy link
Owner

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
Copy link

zac11 commented Sep 5, 2017

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

@dotdashnotdot
Copy link

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;

@JamesMessinger
Copy link
Owner

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
Copy link

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants