2

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

Christian Baumann
  • 1,921
  • 3
  • 15
  • 26
KDSP
  • 23
  • 5

1 Answers1

2

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;
});
Dave Morrison
  • 143
  • 11
  • That is super helpful. Thank you for this and for the explanation as well – KDSP Oct 09 '20 at 10:40
  • Awesome thanks for the rep! You can do cool stuff with postman, It's just javascript. for e.g i created dynamic postman tests for an array of items that came back in response body which asserted that a properties value was set to something, simply by creating a foreach loop that runs pm.test on the iteration item. I love it – Dave Morrison Oct 09 '20 at 11:05