Verify response body contains expected user data
Preconditions (2)
✅ Expected Result: Response status code is 200 and response body JSON contains correct user data as specified
Jump into concepts and practice - no test required
pm.test('Status code is 200', () => { pm.response.to.have.status(200); }); pm.test('Response body has expected user data', () => { const jsonData = pm.response.json(); pm.expect(jsonData).to.have.all.keys('id', 'name', 'email'); pm.expect(jsonData.id).to.eql(123); pm.expect(jsonData.name).to.eql('John Doe'); pm.expect(jsonData.email).to.eql('john.doe@example.com'); });
The first test checks that the response status code is exactly 200 using pm.response.to.have.status(200). This ensures the request was successful.
The second test parses the response body JSON with pm.response.json(). It then asserts the JSON object has all the keys id, name, and email using to.have.all.keys. This confirms the expected data structure.
Finally, it checks each key's value matches the expected data using strict equality to.eql(). This verifies the correctness of the response content.
Each assertion has a clear message to help identify failures during test runs.
Now add data-driven testing with 3 different user IDs and expected user data
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).