Test Overview
This test checks if the API response status is 200 and if the response body contains a specific key. It uses pm.test() in Postman's Tests tab to verify these conditions.
Jump into concepts and practice - no test required
This test checks if the API response status is 200 and if the response body contains a specific key. It uses pm.test() in Postman's Tests tab to verify these conditions.
pm.test("Status code is 200", function () { pm.response.to.have.status(200); }); pm.test("Response has userId", function () { var jsonData = pm.response.json(); pm.expect(jsonData).to.have.property('userId'); });
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts in Postman Tests tab | Postman is ready to run the test scripts after receiving the API response | — | PASS |
| 2 | pm.test("Status code is 200") runs and checks response status | API response received with HTTP status code 200 | Verify response status code equals 200 | PASS |
| 3 | pm.test("Response has userId") runs and checks response body | Response body parsed as JSON, contains key 'userId' | Verify response JSON has property 'userId' | PASS |
pm.test() function in Postman's Tests tab?pm.test() function is used to define a test with a descriptive name and a function that contains checks or assertions.pm.response.to.have.status(200) is the proper way to assert status code 200.pm.test('Response has userId 1', () => {
const jsonData = pm.response.json();
pm.expect(jsonData.userId).to.eql(1);
});{"userId": 2}?jsonData.userId equals 1 using pm.expect().to.eql(1).userId as 2, which does not equal 1, so the assertion fails.pm.test('Check response time', () => {
pm.expect(pm.response.responseTime).to.be.below(200);
});pm.response.responseTime is the correct property for response time in milliseconds, and to.be.below() is valid syntax.responseTime is undefined or not a number, the assertion will fail even if the actual response time is low.items and that each item has a price greater than 0. Which test code correctly implements this?pm.expect(jsonData.items).to.be.an('array') and to.be.greaterThan(0) to verify the array is non-empty.pm.expect(item.price).to.be.greaterThan(0) inside a forEach loop, which is valid and clear.to.be.above(0) which is valid but less common than to.be.greaterThan(0). B uses every() which is valid but chaining to.be.an('array').and.not.empty is not standard Chai syntax in Postman. C has incorrect assertions missing function calls.