Test Overview
This test sends a GET request to a sample API endpoint and verifies that the response body contains the expected user data in JSON format.
This test sends a GET request to a sample API endpoint and verifies that the response body contains the expected user data in JSON format.
pm.test("Response body contains user data", function () { pm.response.to.have.status(200); const jsonData = pm.response.json(); pm.expect(jsonData).to.have.property('user'); pm.expect(jsonData.user).to.have.property('id').that.is.a('number'); pm.expect(jsonData.user).to.have.property('name').that.is.a('string'); });
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Send GET request to https://api.example.com/user/123 | Request sent, waiting for response | — | PASS |
| 2 | Receive response with status 200 and JSON body | Response body: {"user":{"id":123,"name":"Alice"}} | Check response status is 200 | PASS |
| 3 | Parse response body as JSON | Parsed JSON object available | — | PASS |
| 4 | Verify response JSON has property 'user' | JSON contains 'user' object | Assert jsonData has property 'user' | PASS |
| 5 | Verify 'user' object has numeric 'id' | 'user.id' is 123 | Assert user.id is a number | PASS |
| 6 | Verify 'user' object has string 'name' | 'user.name' is 'Alice' | Assert user.name is a string | PASS |
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