Validate response status and content using condition block in Postman test script
Preconditions (2)
✅ Expected Result: Test passes if status code is 200 and userId is 1; otherwise, test fails with appropriate error assertion
pm.test('Condition block test for status and response body', function () { if (pm.response.code === 200) { pm.expect(pm.response.json()).to.have.property('userId', 1); } else { pm.expect(pm.response.text()).to.include('error'); } });
This test script uses a condition block to check the response status code.
If the status code is 200, it asserts that the JSON response has a property userId equal to 1.
If the status code is not 200, it asserts that the response text includes the word error.
This approach ensures the test adapts to different response scenarios and verifies the expected content accordingly.
Now add data-driven testing with 3 different API endpoints and validate their status and response content using condition blocks
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