Validate API response status and body using assertions
Preconditions (2)
✅ Expected Result: The response status code is 200, the body is a JSON array, and the first user object contains a 'name' property
Jump into concepts and practice - no test required
pm.test('Status code is 200', () => { pm.response.to.have.status(200); }); pm.test('Response body is an array', () => { const jsonData = pm.response.json(); pm.expect(jsonData).to.be.an('array'); }); pm.test('First user has a name property', () => { const jsonData = pm.response.json(); pm.expect(jsonData[0]).to.have.property('name'); });
The first test checks the HTTP status code to confirm the request succeeded.
The second test parses the response body as JSON and asserts it is an array, matching the expected data structure.
The third test verifies that the first user object in the array contains a 'name' property, ensuring the data includes expected fields.
Using pm.test and pm.expect provides clear, readable assertions that automatically validate the API response during test runs.
Now add data-driven testing with 3 different API endpoints and validate their responses similarly
pm.test('Check user name', () => {
const jsonData = pm.response.json();
pm.expect(jsonData.name).to.eql('Alice');
});{"name": "Bob"}?pm.test('Response has userId', () => {
pm.expect(pm.response.json().userId).to.exist;
});{"userId": 123}. What is the likely problem?status equal to "active". Which test code correctly validates this?