Test Overview
This test checks if the API response status is 200 and then verifies the presence of a specific field in the JSON body using a condition block in Postman test script.
This test checks if the API response status is 200 and then verifies the presence of a specific field in the JSON body using a condition block in Postman test script.
pm.test("Status code is 200", function () { pm.response.to.have.status(200); }); if (pm.response.code === 200) { pm.test("Response has userId field", function () { var jsonData = pm.response.json(); pm.expect(jsonData).to.have.property('userId'); }); }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | Postman app is open with the API request ready | — | PASS |
| 2 | Sends API request | Request sent to server, waiting for response | — | PASS |
| 3 | Receives response with status 200 | Response body contains JSON data with userId field | pm.response.to.have.status(200) | PASS |
| 4 | Checks if response code is 200 (condition block) | Condition true, enters block to check JSON body | — | PASS |
| 5 | Verifies response JSON has 'userId' property | JSON body includes userId field | pm.expect(jsonData).to.have.property('userId') | PASS |
condition block in Postman tests?if let you run code only when certain conditions are met.if (condition) { } is correct syntax.if with triple equals and braces. Others use invalid keywords or assignment instead of comparison.if (condition) { } in Postman [OK]if (condition) { } syntax [OK]if (pm.response.code === 404) {
pm.test('Not Found', () => {
pm.expect(pm.response.code).to.equal(404);
});
} else {
pm.test('Other status', () => {
pm.expect(pm.response.code).to.not.equal(404);
});
}if checks if code is 404; 200 is not 404, so else block runs.if pm.response.code == 200 {
pm.test('Status is 200', () => {
pm.expect(pm.response.code).to.equal(200);
});
}if, but here they are missing.if (condition) [OK]status field equal to 'success'. If true, test should pass with message 'Success status'. Otherwise, it should fail with message 'Status not success'. Which condition block correctly implements this?if (pm.response.json().status === 'success') with parentheses, strict equality ===, and proper JSON access. Other options lack parens, use assignment, wrong expectations, or swapped logic.pm.test('Success status', () => { pm.expect(true).to.be.true; }) passes. Else, pm.test('Status not success', () => { pm.expect.fail('Status not success'); }) fails with message.if (condition) { pass } else { fail } pattern [OK]Pass the test and this pattern moves unknown