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.