Verify response body contains expected user data
Preconditions (2)
✅ Expected Result: The response body should include the correct user id, name, and email as specified
pm.test('Status code is 200', () => { pm.response.to.have.status(200); }); const responseJson = pm.response.json(); pm.test('User ID is 123', () => { pm.expect(responseJson.id, 'User ID').to.eql(123); }); pm.test('User name is John Doe', () => { pm.expect(responseJson.name, 'User name').to.eql('John Doe'); }); pm.test('User email is john.doe@example.com', () => { pm.expect(responseJson.email, 'User email').to.eql('john.doe@example.com'); });
This script runs inside Postman’s Tests tab after the request is sent.
First, it checks the HTTP status code is 200 to confirm the request succeeded.
Then it parses the response body JSON once and stores it in responseJson for reuse.
Next, it verifies the id, name, and email fields exactly match the expected values using strict equality (to.eql).
Each assertion has a clear label to help identify which check failed if any test does not pass.
Now add data-driven testing with 3 different user IDs and verify their respective names and emails
let jsonData = pm.response.json();
pm.test("Count is 5", function () {
pm.expect(jsonData.count).to.eql(5);
});let data = pm.response.json();
pm.test("User field exists", () => {
pm.expect(data.user).to.exist();
});
The test always fails even when the response contains 'user'.{
"users": [
{"id": 1, "active": true},
{"id": 2, "active": false},
{"id": 3, "active": true}
]
}
Which Postman test code correctly asserts that all users are active?Pass the test and this pattern moves unknown