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!

Thanks bbahadur!

Thanks for the solution; it is exactly what I asked Google to give me.

I posit the following ONLY because I am so Absent-Minded that I just know that I would forget to reset the Initial ‘failed’ value each time I add a test.

Below is a way that I know I won’t mess that up – since I am already on the hook for incrementing ‘passed’ and decrementing ‘failed’ then it isn’t much to increment ‘testCount’ as well.

(I am someone that likes being verbose with variables etc, so in addition to adding ‘testCount’, I have changed ‘passed’ to ‘passedCount’ and ‘failed’ to ‘failedCount’)

let testCount = 0 
let passedCount = 0 
let failedCount = 0 

testCount++
pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
    passedCount++;
    failedCount--;
});
testCount++
pm.test("Response time is less than 500ms", function () {
    pm.expect(pm.response.responseTime).to.be.below(500);
    passedCount++;
    failedCount--;
});
testCount++
pm.test("Response time is less than 5ms", function () {
    pm.expect(pm.response.responseTime).to.be.below(5);
    passedCount++;
    failedCount--;
});
console.log(`passed:    ${passedCount}`);
console.log(`failed:    ${(testCount + failedCount)}`);
console.log(`============`);
console.log(`total:     ${testCount}`);

Pretty sure that works…

Again, thanks for the original solution!

Regards,
Brad

1 Like

This logic is good to implement for 2 test of there are 50 test then its difficult postman has to come with some default variable which can store the overalldata of test result.