What if your tests could think and decide for themselves?
Why Condition block in Postman? - Purpose & Use Cases
Imagine you are testing an API that returns different responses based on user input. You manually check each response by reading the output and deciding what to do next.
This manual checking is slow and tiring. You might miss important cases or make mistakes deciding what to test next. It's like flipping through pages without a clear guide.
Condition blocks let you automate decisions in your tests. They check if something is true or false and run the right steps automatically. This saves time and avoids errors.
if (responseCode === 200) { // manually check success } else { // manually check error }
pm.test('Status is 200', () => { pm.expect(pm.response.code).to.equal(200); });
Condition blocks enable your tests to smartly react to different situations without manual effort.
When testing a login API, a condition block can check if login succeeded and then test user data, or if it failed, test error messages automatically.
Manual checking is slow and error-prone.
Condition blocks automate decision-making in tests.
This leads to faster, more reliable testing.
Practice
condition block in Postman tests?Solution
Step 1: Understand the role of condition blocks
Condition blocks likeiflet 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 BQuick Check:
Condition block = run code if true [OK]
- Confusing condition blocks with request sending
- Thinking condition blocks store variables
- Assuming condition blocks format responses
Solution
Step 1: Identify correct JavaScript condition syntax
Postman test scripts use JavaScript, soif (condition) { }is correct syntax.Step 2: Check each option
if (pm.response.code === 200) { console.log('Success'); } uses correctifwith triple equals and braces. Others use invalid keywords or assignment instead of comparison.Final Answer:
if (pm.response.code === 200) { console.log('Success'); } -> Option DQuick Check:
Useif (condition) { }in Postman [OK]
if (condition) { } syntax [OK]- Using wrong keywords like 'when' or 'check'
- Using single equals '=' instead of '==' or '==='
- Missing parentheses or braces
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);
});
}What test will run if the response code is 200?
Solution
Step 1: Analyze the condition with response code 200
Theifchecks if code is 404; 200 is not 404, soelseblock 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 CQuick Check:
Response 200 triggers else block test [OK]
- Assuming 'Not Found' test runs for 200
- Thinking both tests run simultaneously
- Believing no test runs if condition false
if pm.response.code == 200 {
pm.test('Status is 200', () => {
pm.expect(pm.response.code).to.equal(200);
});
}Solution
Step 1: Check syntax of the if statement
JavaScript requires parentheses around the condition inif, 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 AQuick Check:
Always use parentheses inif (condition)[OK]
- Omitting parentheses around condition
- Confusing == and === as syntax errors
- Expecting semicolons after function calls
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?Solution
Step 1: Check condition syntax and comparison
Correct syntax requiresif (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 AQuick Check:
Use strict equality and fail test in else block [OK]
if (condition) { pass } else { fail } pattern [OK]- Using single equals '=' instead of '==='
- Failing test inside success block
- Passing test inside failure block
Ready to prove it?
Pass the test and this pattern moves unknown
