Multiple test case with a condition

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.

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.

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

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.

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

Hi @mdjones,

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, :+1:
H

1 Like