0
0
Postmantesting~5 mins

Response body array assertions in Postman

Choose your learning style9 modes available
Introduction

We check arrays in response bodies to make sure the data we get is correct and complete. This helps us trust the API works as expected.

When you want to confirm the API returns a list of items, like users or products.
When you need to check that the array has the right number of elements.
When you want to verify specific values exist inside the array.
When you want to ensure each item in the array has certain properties.
When you want to test that the array is not empty before using its data.
Syntax
Postman
pm.test("Test description", function () {
    let jsonData = pm.response.json();
    pm.expect(jsonData.arrayName).to.be.an('array');
    pm.expect(jsonData.arrayName).to.have.lengthOf(expectedLength);
    pm.expect(jsonData.arrayName).to.include(expectedValue);
    pm.expect(jsonData.arrayName[0]).to.have.property('propertyName');
});

Use pm.response.json() to parse the response body as JSON.

Use pm.expect() with Chai assertions to check array properties.

Examples
This checks that the response has a key 'users' and it is an array.
Postman
pm.test("Response has an array called users", function () {
    let jsonData = pm.response.json();
    pm.expect(jsonData.users).to.be.an('array');
});
This checks the 'users' array contains exactly 3 elements.
Postman
pm.test("Users array has 3 items", function () {
    let jsonData = pm.response.json();
    pm.expect(jsonData.users).to.have.lengthOf(3);
});
This checks that among the users, there is one with the name 'Alice'.
Postman
pm.test("Users array includes a user named 'Alice'", function () {
    let jsonData = pm.response.json();
    let userNames = jsonData.users.map(user => user.name);
    pm.expect(userNames).to.include('Alice');
});
This checks the first user object has an 'id' property.
Postman
pm.test("First user has an 'id' property", function () {
    let jsonData = pm.response.json();
    pm.expect(jsonData.users[0]).to.have.property('id');
});
Sample Program

This test checks that the response has an 'items' array with exactly 2 elements. It also verifies that one of the items is named 'Book' and that the first item has a 'price' property.

Postman
pm.test("Validate response body array assertions", function () {
    let jsonData = pm.response.json();
    
    // Check 'items' is an array
    pm.expect(jsonData.items).to.be.an('array');
    
    // Check array length is 2
    pm.expect(jsonData.items).to.have.lengthOf(2);
    
    // Check array includes an item with name 'Book'
    let itemNames = jsonData.items.map(item => item.name);
    pm.expect(itemNames).to.include('Book');
    
    // Check first item has 'price' property
    pm.expect(jsonData.items[0]).to.have.property('price');
});
OutputSuccess
Important Notes

Array assertions help catch errors early by confirming the structure and content of response data.

Common mistake: Forgetting to parse the response body as JSON before assertions.

Use array assertions when you expect multiple items, and use object assertions for single items.

Summary

Response body array assertions check if the API returns the correct list of items.

We verify array type, length, contents, and properties of items inside.

These checks help ensure the API data is reliable and complete.