How to validate multiple different data from postman response

Hi, Can some one help on my issue.
I have multiple responses (Around 70) in single request. How to validate this when we have multiple responses. Below i have attached the Image. in that I have responses for stateProvinces.

1 Like

Hi @bharath4172

You are working inside an array so you would need to specify the index you are asserting.

const response = pm.response.json();

pm.test("Check stateDesc values", function () {
    pm.expect(response.stateDesc[0].countryName).to.eql("Michigan");
    pm.expect(response.stateDesc[1].countryName).to.eql("Idaho");
    pm.expect(response.stateDesc[2].countryName).to.eql("Colorado");
    pm.expect(response.stateDesc[3].countryName).to.eql("Utah");
});
2 Likes

Not sure why this post come to the top of the list as its over six months old.

However, purely from a testing perspective, I would not recommend asserting on 4 elements in one go like this.

If the first one fails, then the others will not run.

It breaks the singular rule.

These should be singular tests. It should be possible to create a loop to loop through the response, so it will run once for each element. That way, if more than one fails, it will show in the results.

This all depends on what you are trying to assert. You could use the JavaScript map function to save all of the stateCodes into a new array, and then just check if the state codes exist “pm.expect(array).to.include(countryName)”.

Please don’t use “pm.expect(array).to.have.members([‘Michigan’,‘Idaho’])” as that has the same issue in respect to testing more than one thing at at time.

You could also do a find to see if the element exists in the array. In this example, stateProvinces is an array, so you should be able to do this direct against that array and all you do then is assert that the response is not null or undefined.

Debatable, If the criteria you are testing against are that ALL should be as expected then if one item is missing/incorrect, you would want the test to fail.

Bare in mind that the example above was derived from the official Postman docs;

Test approaches are debatable and best practice is always subjective.

But from experience, its best to have individual tests.

It doesn’t need to be a lot more code. Just define the expected results in an array, and then put your test in a loop. This way, all tests are executed, so your report will show you all of the potential defects instead of erroring on the first test and then not providing feedback on the other tests. You may have more than one bug, which is not being surfaced by the test.