Verify response body contains expected array elements
Preconditions (2)
✅ Expected Result: The response status code is 200, the response body is an array, it contains a user with id 1, and all user objects have the required properties
pm.test('Status code is 200', () => { pm.response.to.have.status(200); }); const jsonData = pm.response.json(); pm.test('Response body is an array', () => { pm.expect(Array.isArray(jsonData)).to.be.true; }); pm.test('Array contains a user with id = 1', () => { const hasUserId1 = jsonData.some(user => user.id === 1); pm.expect(hasUserId1, 'User with id 1 should exist').to.be.true; }); pm.test('Each user has id, name, and email properties', () => { const allHaveProps = jsonData.every(user => user.hasOwnProperty('id') && user.hasOwnProperty('name') && user.hasOwnProperty('email') ); pm.expect(allHaveProps, 'All users should have id, name, and email').to.be.true; });
This test script uses Postman’s built-in pm object to write assertions.
First, it checks the status code is 200 to confirm the request succeeded.
Then it parses the response JSON once and stores it in jsonData for reuse.
It asserts the response is an array using Array.isArray().
Next, it uses some() to check if any user object has id equal to 1.
Finally, it uses every() to verify all user objects have the required properties id, name, and email.
Each assertion has a clear message to help understand test results.
Now add data-driven testing with 3 different user IDs to verify each exists in the response array
[{"id":1,"name":"Alice"},{"id":2,"name":"Bob"} ], what will this Postman test output? pm.test('Check length', () => {
const jsonData = pm.response.json();
pm.expect(jsonData.length).to.eql(3);
});pm.test('All items have id', () => {
const jsonData = pm.response.json();
jsonData.forEach(item => {
pm.expect(item.id).to.exist;
});
});status equal to active. Which Postman test code correctly does this?Pass the test and this pattern moves unknown