A condition block lets you run code only when certain things are true. It helps you check if your API response is correct or handle different cases.
Check if an API response status is 200 before testing the data.
Run different tests depending on the value of a response field.
Skip tests when a required field is missing in the response.
Log a message only if an error occurs in the response.
Syntax
Postman
if (condition) {
// code to run if condition is true
} else {
// code to run if condition is false
}
The condition is a statement that is either true or false.
The else part is optional and runs when the condition is false.
Examples
This runs a test only if the response status code is 200.
Postman
if (pm.response.code === 200) {
pm.test('Status is 200', () => {
pm.expect(pm.response.code).to.equal(200);
});
}
This logs a message depending on the success field in the JSON response.
Postman
if (pm.response.json().success) {
console.log('Success is true');
} else {
console.log('Success is false');
}
Sample Program
This test checks if the user is active. It runs different assertions and logs messages based on the active field.
Postman
pm.test('Check user is active', () => {
let jsonData = pm.response.json();
if (jsonData.active) {
pm.expect(jsonData.active).to.be.true;
console.log('User is active');
} else {
pm.expect(jsonData.active).to.be.false;
console.log('User is not active');
}
});
OutputSuccess
Important Notes
Use strict equality === to avoid unexpected type conversions.
Always check if the response data exists before accessing fields to avoid errors.
Summary
Condition blocks let you run code only when certain things are true.
They help make tests smarter by handling different response cases.
Use if and optional else to control test flow.
Practice
(1/5)
1. What is the main purpose of a condition block in Postman tests?
easy
A. To send multiple requests at once
B. To run code only when a specific condition is true
C. To store variables permanently
D. To format the response body
Solution
Step 1: Understand the role of condition blocks
Condition blocks like if let you run code only when certain conditions are met.
Step 2: Compare with other options
Sending requests, storing variables, or formatting responses are different features, not condition blocks.
Final Answer:
To run code only when a specific condition is true -> Option B
Quick Check:
Condition block = run code if true [OK]
Hint: Condition blocks run code only if conditions are true [OK]
Common Mistakes:
Confusing condition blocks with request sending
Thinking condition blocks store variables
Assuming condition blocks format responses
2. Which of the following is the correct syntax for a condition block in Postman test scripts?
easy
A. check (pm.response.code === 200) { console.log('Success'); }
B. when (pm.response.code == 200) { console.log('Success'); }
C. if pm.response.code = 200 then { console.log('Success'); }
D. if (pm.response.code === 200) { console.log('Success'); }
Postman test scripts use JavaScript, so if (condition) { } is correct syntax.
Step 2: Check each option
if (pm.response.code === 200) { console.log('Success'); } uses correct if with triple equals and braces. Others use invalid keywords or assignment instead of comparison.
Final Answer:
if (pm.response.code === 200) { console.log('Success'); } -> Option D
Quick Check:
Use if (condition) { } in Postman [OK]
Hint: Use JavaScript if (condition) { } syntax [OK]
Step 1: Analyze the condition with response code 200
The if checks if code is 404; 200 is not 404, so else block runs.
Step 2: Identify which test runs in else block
The else block runs a test named 'Other status' that expects code not to be 404, which is true for 200.
Final Answer:
'Other status' test runs and passes -> Option C
Quick Check:
Response 200 triggers else block test [OK]
Hint: If condition false, else block runs test [OK]
Common Mistakes:
Assuming 'Not Found' test runs for 200
Thinking both tests run simultaneously
Believing no test runs if condition false
4. Identify the error in this Postman test script condition block:
if pm.response.code == 200 {
pm.test('Status is 200', () => {
pm.expect(pm.response.code).to.equal(200);
});
}
medium
A. Missing parentheses around the condition
B. Using double equals instead of triple equals
C. Missing semicolon after pm.test
D. Incorrect function arrow syntax
Solution
Step 1: Check syntax of the if statement
JavaScript requires parentheses around the condition in if, but here they are missing.
Step 2: Verify other syntax elements
Double equals is allowed in JS, semicolon after pm.test is optional, and arrow function syntax is correct.
Final Answer:
Missing parentheses around the condition -> Option A
Quick Check:
Always use parentheses in if (condition) [OK]
Hint: Always wrap condition in parentheses in if statements [OK]
Common Mistakes:
Omitting parentheses around condition
Confusing == and === as syntax errors
Expecting semicolons after function calls
5. You want to write a Postman test that checks if the response JSON has a 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?
hard
A. if (pm.response.json().status === 'success') {
pm.test('Success status', () => {
pm.expect(true).to.be.true;
});
} else {
pm.test('Status not success', () => {
pm.expect.fail('Status not success');
});
}
B. if pm.response.json().status = 'success' {
pm.test('Success status', () => {
pm.expect(true).to.be.true;
});
} else {
pm.test('Status not success', () => {
pm.expect.fail('Status not success');
});
}
C. if (pm.response.json().status == 'success') {
pm.test('Success status', () => {
pm.expect(false).to.be.true;
});
} else {
pm.test('Status not success', () => {
pm.expect.fail('Status not success');
});
}
D. if (pm.response.json().status === 'success') {
pm.test('Success status', () => {
pm.expect.fail('Status not success');
});
} else {
pm.test('Status not success', () => {
pm.expect(true).to.be.true;
});
}
Solution
Step 1: Check condition syntax and comparison
Correct syntax requires 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.
Step 2: Verify test pass/fail logic
If true, 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.
Final Answer:
if (pm.response.json().status === 'success') {
pm.test('Success status', () => {
pm.expect(true).to.be.true;
});
} else {
pm.test('Status not success', () => {
pm.expect.fail('Status not success');
});
} -> Option A
Quick Check:
Use strict equality and fail test in else block [OK]
Hint: Use if (condition) { pass } else { fail } pattern [OK]
Common Mistakes:
Using single equals '=' instead of '==='
Failing test inside success block
Passing test inside failure block
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.