Verify HTTP status codes for API responses
Preconditions (2)
✅ Expected Result: The first request returns status code 200 OK, the second request returns status code 404 Not Found
pm.test('Status code is 200 for valid post', () => { pm.response.to.have.status(200); }); pm.sendRequest('https://jsonplaceholder.typicode.com/posts/invalid', (err, res) => { pm.test('Status code is 404 for invalid post', () => { pm.expect(res).to.have.property('code', 404); }); });
The first test checks that the response status code for a valid GET request is 200, which means OK.
The second test sends a request to an invalid URL and verifies the status code is 404, meaning Not Found.
Using pm.response.to.have.status() and pm.expect() ensures clear and reliable assertions.
Tests are independent and use Postman's built-in test functions for clarity.
Now add data-driven testing with 3 different API endpoints and verify their status codes
200 usually mean in Postman response testing?pm.test('Check status', () => {
pm.expect(pm.response.code).to.be.oneOf([200, 201]);
});pm.test('Status is 200', () => {
pm.expect(pm.response.code = 200);
});Pass the test and this pattern moves unknown