# Combining multiple assertion steps into a single step

**URL:** <https://community.postman.com/t/combining-multiple-assertion-steps-into-a-single-step/59879>\
**Category:** Help Hub\
**Tags:** responseassertion\
**Created:** [April 12, 2024, 9:22am UTC](https://community.postman.com/t/combining-multiple-assertion-steps-into-a-single-step/59879 "2024-04-12T09:22:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kayal1postman](https://avatars.discourse-cdn.com/v4/letter/k/a698b9/32.png) [@kayal1postman](https://community.postman.com/u/kayal1postman)\
**Post date:** [April 12, 2024, 9:22am UTC](https://community.postman.com/t/combining-multiple-assertion-steps-into-a-single-step/59879/1 "2024-04-12T09:22:40Z")

</div>

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”);

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [April 12, 2024, 11:20am UTC](https://community.postman.com/t/combining-multiple-assertion-steps-into-a-single-step/59879/2 "2024-04-12T11:20:34Z")

</div>

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

```auto
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.

```auto
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

---

<div class="post-metadata">

**Author:** ![kayal1postman](https://avatars.discourse-cdn.com/v4/letter/k/a698b9/32.png) [@kayal1postman](https://community.postman.com/u/kayal1postman)\
**Post date:** [April 12, 2024, 1:41pm UTC](https://community.postman.com/t/combining-multiple-assertion-steps-into-a-single-step/59879/3 "2024-04-12T13:41:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [May 12, 2024, 1:41pm UTC](https://community.postman.com/t/combining-multiple-assertion-steps-into-a-single-step/59879/4 "2024-05-12T13:41:57Z")

</div>

Closed on May 12, 2024, 1:41pm UTC

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