Postman test - How to fetch failure count

Hi

I am looking for a way to fetch the number of passed test count. Based on the count, I will have to do certain action. I do not want if the request failed or Passed which I can do it using a counter variable / storing the value in environment variable etc.

For example, I have a collection with 5 requests. Each request has say 10 tests (assertions) in the Tests tab. I need to perform certain things if the passed count is not equal to 10.

Blockquote

Collection A → 5 requests ->Each request has say 10 tests
Req1 → 10 Tests
Req 2 → 20 Tests
…Req10
I need to fetch the failure count / Passed test count for Req1.

Blockquote

I searched the documentation. I did not find anything. Please share some pointers.
Note: Per postman support team’s suggestion, I am posting it here.

Regards, Raje

Hey @rjayaram!

To know the number of passed or failed tests per request, you can use the counter variable as well.
Inside your Assertion / test method just increment/ decrement the counter and at the end of all tests, you’ll have all the tests that passed or failed! you can then either store this counter in environment or collection variable as per your wish.

Here is a simple example

var passed=0, failed =2; //initially failed has the number of test you will have in the request

pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
    passed++;
    failed--;
});
pm.test("Response time is less than 200ms", function () {
    pm.expect(pm.response.responseTime).to.be.below(100);
     passed++;
    failed--;
});
console.log('Passed=',passed,' Failed=',failed);

Make sure you do the increment/decrement after the test/assertion statement, as it determines whether the following operation executes or not.

Hope it helps,
Good luck!

Hi bbahadur

Thanks for looking into this and providing tips. I am not using pm. tests…
All my tests are something like this: will the counter variable way(incrementing counter after each test) still work? Pl confirm.

tests["Test 6: Response has bbbb] = responseBody.has(“bbbbb”);

Thanks, Raje

Hey @rjayaram,

The problem with using just assertions is that the execution stops if the assertion fails, and remaining script is not executed, So it is better to enclose the assertions inside the pm.test method.
BUT, if you still want to do it this way, one option is to use try-catch block.
You need to enclose all your assertions within a try-catch and do the counter increment/decrement.
For reference, something like this:

var passed=0, failed =2;
 try{
    if(pm.response.to.have.status(200)){
          passed++;
          failed--;
       }
    }
    catch(error){
        console.log(error);
    }

    try{
    if(pm.expect(pm.response.responseTime).to.be.below(100)){
        passed++;
        failed--;
       }
    }
    catch(error){
        console.log(error);
    }

       console.log('passed=',passed,'failed=',failed);

Good luck!