Is there any way to continue with tests in postman even if any one assert fails in it?

In a Postman GET request, I have used multiple assertions. However, if any assertion fails, Postman will stops its execution then and there and will throw an error.

Is there any way to prevent this and continue with other assertions even if any one among them fails?

pm.test(“Verify the Usage amount for Text”, function () {
pm.response.to.have.status(200);
var jsonData = pm.response.json() ;
pm.expect(responseBody.length).to.eql(3);
pm.expect(jsonData, “response is NOT empty”).not.to.be.empty;
});
like in above example, I wanted to test all 3 assertion and than fail the tests.
Currently if it fails on first assertion, it stops the test and showed the result only for first assertion without testing rest two assertions.

try putting each validation in its own pm.test…

pm.test(“Verify the Usage amount for Text”, function () {
pm.response.to.have.status(200);
});

pm.test(“Verify the Usage amount for Text”, function () {
var jsonData = pm.response.json() ;
pm.expect(responseBody.length).to.eql(3);
});

pm.test(“Verify the Usage amount for Text”, function () {
pm.expect(jsonData, “response is NOT empty”).not.to.be.empty;
});

Thanks for replying.Yes that is the one way, but I wanted to test all in one test command so that Total Test count will remain one in the report.

If you run multi tests it fails like this with 1 output right? …


So then if you add try / catch statements like this example;

pm.test("API response contains the expected fields", () => {

const response = pm.response.json(); 

try{pm.expect(response).to.have.nested.property("category.name", "WRONG DATA");  
}catch(e){pm.test("A Test Failed", () => {throw new Error(e.message)});}

});

It will throw the error for each test that fails but continue through like so…

This is all within 1 pm.test so should display as 1 in the report as requested…
Hope this helps

1 Like

Tests is always pass even assertion is failed. Is there any workaround for this??

Add this above the try catch

let count = 0;

And add this at the end of the try catch;

    if(count > 0) {
        pm.expect.fail("There are " + count + " failures within the above checks, please check...")
    } 

Hi,
also looking for that solution, but where exactly? I tried it, and it doesn’t do a thing…

Hi @marcelsmit
I’m not sure what you mean by “doesn’t do a thing” … could you please share what you have tried?

Here is a screenshot of my code above working;
(Note; 2 of the try/catch statements are set to fail and then I use pm.expect.fail to fail the overall test)

And here is a screenshot when the try/catch has the correct data;

Sorry for my short question without further explanation.
My point was that I used the try/catch in your example. and that didn’t do much. The earlier example of a year ago of course did well.
But now that I see your screenshot I realize I forgot something to add.: )}), count++;} with every check

1 Like