Validate API response correctness using Postman
Preconditions (2)
✅ Expected Result: The API response status code is 200 and the response body matches the expected data confirming the API works correctly
pm.test('Status code is 200', () => { pm.response.to.have.status(200); }); pm.test('Response body has expected fields', () => { const jsonData = pm.response.json(); pm.expect(jsonData).to.have.property('id'); pm.expect(jsonData).to.have.property('name'); pm.expect(jsonData.name).to.eql('John Doe'); });
The first test checks if the response status code is exactly 200, which means the request was successful.
The second test parses the response body as JSON and checks if it contains the expected properties 'id' and 'name'. It also verifies that the 'name' property equals 'John Doe'.
These checks confirm the API returns the correct data, ensuring response validation confirms correctness.
Now add data-driven testing with 3 different expected 'name' values for the response body
pm.test('Check status', () => {
pm.response.to.have.status(200);
});pm.test('Check name field', () => {
pm.expect(pm.response.json().name).to.equal;
});Pass the test and this pattern moves unknown