Combining multiple assertion steps into a single step

Hi ,
Below is my multiple lines of code for asserting whether object got these properties. Is there any way that I can rewrite into a single line code please. your assistance is much appreciated.

pm.expect(json.Risk).to.have.property(“XXXXXX”);
pm.expect(json.Risk).to.have.property(“YYYYY”);
pm.expect(json.Risk).to.have.property(“ZZZZ”);
pm.expect(json.Risk).to.have.property(“AAAAA”);

If Risk is an array, you can use “to.have.members”.

pm.expect(response.risk).to.have.members(['XXXXX','YYYYY','ZZZZZ','AAAAA']);

If Risks is not an array, you can use Object.keys to return an array of the keys.

const response = pm.response.json();

pm.test("Response has correct keys", () => {
    pm.expect(Object.keys(response.risk)).to.have.members(['XXXXX','YYYYY','ZZZZZ','AAAAA']);
});

Please note. This type of test can be problematic. If you have multiple failing keys then its not going to be able to display this very well in the test results.

Its ok if the JSON is simple, but can be an issue if the JSON is complex.

It might better to have several tests, each testing for a single element, which then becomes obvious which element is missing

Understood and noted @michaelderekjones . Clearly a great explanation. It worked , thanks

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.