Bird
Raised Fist0
Postmantesting~20 mins

Condition block in Postman - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Condition Block Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Postman test script?
Consider this Postman test script that checks the response status code and sets a variable accordingly. What will be the value of the variable testResult after running this script if the response status code is 404?
Postman
if (pm.response.code === 200) {
    pm.variables.set('testResult', 'Success');
} else if (pm.response.code === 404) {
    pm.variables.set('testResult', 'Not Found');
} else {
    pm.variables.set('testResult', 'Other Error');
}
A"Not Found"
Bundefined
C"Other Error"
D"Success"
Attempts:
2 left
💡 Hint
Check the condition that matches the response code 404.
❓ assertion
intermediate
2:00remaining
Which assertion correctly tests if the response JSON has a key 'status' with value 'ok'?
You want to write a Postman test that asserts the JSON response has a key status equal to ok. Which option is the correct assertion?
Apm.test('Status is ok', () => { pm.expect(pm.response.json().status).to.be.undefined; });
Bpm.test('Status is ok', () => { pm.expect(pm.response.json().status).to.eql('ok'); });
Cpm.test('Status is ok', () => { pm.expect(pm.response.json().status).to.be.true; });
Dpm.test('Status is ok', () => { pm.expect(pm.response.json().status).to.be.null; });
Attempts:
2 left
💡 Hint
Check which assertion compares the value exactly to 'ok'.
🔧 Debug
advanced
2:00remaining
Why does this Postman test script fail to set the variable correctly?
This script is intended to set the variable result to 'Passed' if the response code is 201, but it never sets it. What is the error?
Postman
if (pm.response.code = 201) {
    pm.variables.set('result', 'Passed');
} else {
    pm.variables.set('result', 'Failed');
}
AThe single '=' is an assignment, should be '===' for comparison.
BThe variable 'result' is not declared before setting.
Cpm.variables.set cannot be used inside if blocks.
DThe response code should be accessed with pm.response.statusCode.
Attempts:
2 left
💡 Hint
Check the operator used in the if condition.
❓ framework
advanced
2:00remaining
Which Postman test script correctly uses a condition block to check multiple response codes?
You want to write a Postman test that passes if the response code is 200 or 201, and fails otherwise. Which script correctly implements this?
Apm.test('Status is 200 or 201', () => { if (pm.response.code === 200 || pm.response.code === 201) { pm.expect(true).to.be.true; } else { pm.expect.fail('Unexpected status code'); } });
Bpm.test('Status is 200 or 201', () => { pm.expect(pm.response.code === 200 || 201).to.be.true; });
Cpm.test('Status is 200 or 201', () => { if (pm.response.code === 200 && pm.response.code === 201) { pm.expect(true).to.be.true; } else { pm.expect.fail('Unexpected status code'); } });
Dpm.test('Status is 200 or 201', () => { pm.expect(pm.response.code).to.be.oneOf([200, 201]); });
Attempts:
2 left
💡 Hint
Check how to test if a value is one of multiple options.
🧠 Conceptual
expert
2:00remaining
What is the main advantage of using condition blocks in Postman test scripts?
Why do testers use condition blocks (like if-else) in Postman test scripts when validating API responses?
ATo speed up the API response time by skipping some requests.
BTo automatically generate API documentation from test scripts.
CTo execute different test assertions based on varying response data or status codes.
DTo encrypt sensitive data in the response before testing.
Attempts:
2 left
💡 Hint
Think about how tests adapt to different responses.

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

  1. Step 1: Understand the role of condition blocks

    Condition blocks like if let you run code only when certain conditions are met.
  2. Step 2: Compare with other options

    Sending requests, storing variables, or formatting responses are different features, not condition blocks.
  3. Final Answer:

    To run code only when a specific condition is true -> Option B
  4. 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'); }

Solution

  1. Step 1: Identify correct JavaScript condition syntax

    Postman test scripts use JavaScript, so if (condition) { } is correct syntax.
  2. 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.
  3. Final Answer:

    if (pm.response.code === 200) { console.log('Success'); } -> Option D
  4. Quick Check:

    Use if (condition) { } in Postman [OK]
Hint: Use JavaScript if (condition) { } syntax [OK]
Common Mistakes:
  • Using wrong keywords like 'when' or 'check'
  • Using single equals '=' instead of '==' or '==='
  • Missing parentheses or braces
3. Given this Postman test script snippet:
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?
medium
A. 'Not Found' test runs and passes
B. Both tests run and fail
C. 'Other status' test runs and passes
D. No tests run

Solution

  1. Step 1: Analyze the condition with response code 200

    The if checks if code is 404; 200 is not 404, so else block runs.
  2. 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.
  3. Final Answer:

    'Other status' test runs and passes -> Option C
  4. 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

  1. Step 1: Check syntax of the if statement

    JavaScript requires parentheses around the condition in if, but here they are missing.
  2. Step 2: Verify other syntax elements

    Double equals is allowed in JS, semicolon after pm.test is optional, and arrow function syntax is correct.
  3. Final Answer:

    Missing parentheses around the condition -> Option A
  4. 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

  1. 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.
  2. 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.
  3. 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
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard