# Multiple test case with a condition

**URL:** <https://community.postman.com/t/multiple-test-case-with-a-condition/41166>\
**Category:** Help Hub\
**Tags:** tests\
**Created:** [November 16, 2022, 2:41pm UTC](https://community.postman.com/t/multiple-test-case-with-a-condition/41166 "2022-11-16T14:41:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Maawiy](https://avatars.discourse-cdn.com/v4/letter/m/74df32/32.png) [@Maawiy](https://community.postman.com/u/Maawiy)\
**Post date:** [November 16, 2022, 2:41pm UTC](https://community.postman.com/t/multiple-test-case-with-a-condition/41166/1 "2022-11-16T14:41:31Z")

</div>

Hello World!

After my request, I’d like to test that all fields are present in the response. I have 60 different fields.

My test checks if multiple fields are included in the response body. If an expected field is missing, from the response, my test fails.

I’d like to add a condition. If an expected field is missing, i’d like the specific field highlighted/ flagged. This way I don’t have to manually check off each line to find the missing field.

Below is an example of my test…

const response=pm.response.json();  
pm.test(“All Fields Present”,() =\> {  
pm.expect(pm.response.text()).to.include(“title”);  
pm.expect(pm.response.text()).to.include(“first\_name”);  
pm.expect(pm.response.text()).to.include(“surname”);  
pm.expect(pm.response.text()).to.include(“date”);  
pm.expect(pm.response.text()).to.include(“time”);  
pm.expect(pm.response.text()).to.include(“email”);  
pm.expect(pm.response.text()).to.include(“post code”);

Below is a sample of my response body  
{  
“title”: “Mr”,  
“first\_name”: “name”,  
“surname”: “surname”,  
“date”: “16 Nov 2022”,  
“time”: “13:57:58”,  
“email”: “”,  
“post code”: “”  
}

There are 50+ other fields included. I’d like to go further and have the test result flag up a missing expected field…

Any suggestions, if there is a better/ easier way would be greatly appreciated.

---

<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:** [November 16, 2022, 6:10pm UTC](https://community.postman.com/t/multiple-test-case-with-a-condition/41166/2 "2022-11-16T18:10:05Z")

</div>

You’ve discovered the reason why each test needs to be singular as possible.

However, if you are testing just the structure of the JSON, then you can do something similar to the following (which is covered in the Learning Centre training BTW).

This example verifies the JSON structure for an OAuth connection.

```auto
response = pm.response.json();

//Step 1: Define the schema

const schema = {
    'type': 'object',
    'properties': {
        'token_type': {
            type: 'string'
        },
        'scope': {
            type: 'string'
        },
        'expires_in': {
            type: 'number'
        },
        'ext_expires_in': {
            type: 'number'
        },
        'access_token': {
            type: 'string'
        },
        'id_token': {
            type: 'string'
        }
    },
    required: ['token_type', 'scope', 'expires_in', 'ext_expires_in', 'access_token', 'id_token']
};

//Step 2: Validate response against schema

if(pm.response.code===200)
    pm.test('MicrosoftOnline Login response schema is valid', () => {
        pm.expect(response).to.have.jsonSchema(schema);
    });

```

---

<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:** [November 16, 2022, 6:16pm UTC](https://community.postman.com/t/multiple-test-case-with-a-condition/41166/3 "2022-11-16T18:16:59Z")

</div>

You also might want to consider the following approach, which loops through an array, so you only have to write the test once. One test run multiple times, which resolves the singular test issue.

```auto
var array = ["Title", "First_Name", "Surname", "email"];

// for loop

for (let i = 0; i < array.length; i++) {

    var field = array[i];
    // console.log(id);
    
    pm.test(`for loop expect ${field} to be a string`, () => {
        pm.expect(field).to.be.an("string");
    });

};

// for each loop

array.forEach(field => {
    
    pm.test(`forEach loop expect ${field} to be an string`, () => {
        pm.expect(field).to.be.an("string");
    });
    
});

```

![image](https://us1.discourse-cdn.com/getpostman/original/3X/3/6/364c4644e8d1734e65b0d9a0cabd5371cc9cac15.png)

---

<div class="post-metadata">

**Author:** ![Maawiy](https://avatars.discourse-cdn.com/v4/letter/m/74df32/32.png) [@Maawiy](https://community.postman.com/u/Maawiy)\
**Post date:** [November 17, 2022, 3:19pm UTC](https://community.postman.com/t/multiple-test-case-with-a-condition/41166/4 "2022-11-17T15:19:46Z")

</div>

Hi @michaelderekjones,

Thank you very much for taking the time to get back to my post, learnt a lot.

I tested both examples and both work just fine. I’ve implemented the first example but will keep the second in the back pocket.

Thank you, 👍  
H
