POSTMAN - how to check response body for a value > 0

622 views Asked by At

I'm fairly new to postman and am struggling to find an answer to the below

I have a query that returns data such as the below:

{
   "dailyProcessing":{
      "2020-10-01":{
         "Database":{
            "list0":0,
            "list1":0,
            "list2":300,
            "list3":0,
            "list4":0
         }
      },
      "2020-10-02":{
         "Database":{
            "list0":0,
            "list1":0,
            "list2":0,
            "list3":0,
            "list4":400
         }
      }
   }
}

I need to write a test that checks that one of those values returns a number >0 and am not sure what the format should be.

The list used will be random, so it can't be specific to one list but there will always be one being used, and we need to flag if we are not seeing a value greater than 0 in the response as it will identify an issue with processing

I have a small amount of knowledge so have tried putting things together like

pm.expect(pm.response.text()).to.include

but I don't seem to be getting the result I need

Apologies if this is a really simple thing, I do not have the knowledge yet to know what exactly it is (i.e the terminology) that I am trying to do

Thanks in advance

1

There are 1 answers

2
Dave Morrison On BEST ANSWER

Hope this code helps you, put it in the Test tab

It will loop through each of the properties, navigating to the Database property and then loop through each of the list items.

It will do this for all of the available items until a condition is met, the condition being that the list item value is > 0.

Once the condition is met, it will break from both loops and then assert that the condition met is true.

If all values are 0 then and the loop completes, then the condition will not be met and you test will fail.

var dailyProcessing = JSON.parse(responseBody);
var conditionMet = false;

for (var key in dailyProcessing)
{
    console.log(Object.keys(dailyProcessing).length)
    console.log(key)

    for(var item in dailyProcessing[key].Database)
    {
        if(dailyProcessing[key].Database[item] > 0){
            conditionMet = true;
            break;
        }
    }

    if(conditionMet){
        break;
    }
}

pm.test("Condition is met", function() {
    pm.expect(conditionMet).to.be.true;
});