Test Overview
This test sends a GET request to an API endpoint and verifies that the response body contains the expected user data, including the user's name and email.
Jump into concepts and practice - no test required
This test sends a GET request to an API endpoint and verifies that the response body contains the expected user data, including the user's name and email.
pm.test("Response body contains correct user data", function () { const responseJson = pm.response.json(); pm.expect(responseJson).to.have.property('name', 'John Doe'); pm.expect(responseJson).to.have.property('email', 'john.doe@example.com'); });
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | Postman is ready to send the request | — | PASS |
| 2 | Send GET request to API endpoint | Request sent to https://api.example.com/users/123 | — | PASS |
| 3 | Receive response with status 200 and JSON body | Response body: {"id":123,"name":"John Doe","email":"john.doe@example.com"} | Status code is 200 | PASS |
| 4 | Parse response body as JSON | Parsed JSON object available for assertions | — | PASS |
| 5 | Assert response JSON has property 'name' with value 'John Doe' | Checking 'name' property in response JSON | responseJson.name === 'John Doe' | PASS |
| 6 | Assert response JSON has property 'email' with value 'john.doe@example.com' | Checking 'email' property in response JSON | responseJson.email === 'john.doe@example.com' | PASS |
| 7 | Test ends | All assertions passed | — | PASS |
pm.response.json() do in Postman tests?pm.response.json()pm.response.json().status with value success in Postman?pm.expect() with Chai assertion style, so pm.expect(pm.response.json().status).to.eql('success'); is correct.{"user":{"id":5,"name":"Alice"}} What will this test output?const jsonData = pm.response.json();
pm.test("User ID is 5", () => {
pm.expect(jsonData.user.id).to.equal(5);
});jsonData.user.id is 5.jsonData.user.id equals 5, which is true, so the test passes.const data = pm.response.json();
pm.test("Check user name", () => {
pm.expect(data.user.name).to.equal('Bob')
});pm.test correctly, with proper arrow function and assertion syntax.items contains an object with id equal to 10. Which test code correctly checks this in Postman?items is an array of objects; we want to check if any object has id 10.some() to check if any item has id === 10, which is correct. pm.expect(pm.response.json().items.id).to.equal(10); wrongly accesses items.id (invalid). pm.expect(pm.response.json().items.includes({id:10})).to.be.true; tries to use includes() with an object, which won't work. const items = pm.response.json().items;
pm.expect(items.find(id => id === 10)).to.exist; uses find() but the callback is incorrect (should check item.id).