Bird
Raised Fist0
Postmantesting~20 mins

Default and conditional responses in Postman - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Master of Default and Conditional Responses
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the test result of this Postman test script?
Given the following Postman test script, what will be the test result if the response status code is 404?
Postman
pm.test('Status code is 200 or 201', function () {
    pm.expect(pm.response.code).to.be.oneOf([200, 201]);
});
ATest fails because 404 is not in [200, 201]
BTest passes because 404 is considered a success
CTest throws an error due to invalid syntax
DTest is skipped automatically
Attempts:
2 left
💡 Hint
Check which status codes are accepted by the oneOf assertion.
assertion
intermediate
2:00remaining
Which assertion correctly checks for a default JSON property in Postman?
You want to verify that the JSON response always contains a property named 'success' with a boolean value. Which assertion is correct?
Apm.expect(pm.response.json().success).to.be.a('boolean');
Bpm.expect(pm.response.json().success).to.be.true || false;
Cpm.expect(pm.response.json().success).to.equal('true');
Dpm.expect(pm.response.json().success).to.be('boolean');
Attempts:
2 left
💡 Hint
Check the syntax for type checking in Postman assertions.
🧠 Conceptual
advanced
2:00remaining
What happens if no conditional test matches in a Postman test script?
In a Postman test script, you have multiple conditional tests checking response codes 200, 201, and 404. What is the default behavior if the response code is 500 and no condition matches?
ANo test runs and the test suite passes by default
BAll tests fail automatically because no condition matched
CPostman throws a runtime error due to unmatched conditions
DTests not matching conditions are skipped, but overall test result depends on other tests
Attempts:
2 left
💡 Hint
Think about how Postman handles tests that are not explicitly triggered.
🔧 Debug
advanced
2:00remaining
Why does this Postman test script always fail?
Analyze the following Postman test script and identify why it always fails regardless of response status code: pm.test('Check status', () => { if (pm.response.code === 200) { pm.expect(true).to.be.true; } else if (pm.response.code === 404) { pm.expect(true).to.be.true; } });
Postman
pm.test('Check status', () => {
  if (pm.response.code === 200) {
    pm.expect(true).to.be.true;
  } else if (pm.response.code === 404) {
    pm.expect(true).to.be.true;
  }
});
AThe test always fails because pm.expect(true).to.be.true is incorrect
BThe test syntax is invalid causing failure
CThe test fails when response code is neither 200 nor 404 because no assertion runs
DThe test fails because pm.response.code is undefined
Attempts:
2 left
💡 Hint
What happens if the response code is 500?
framework
expert
3:00remaining
How to implement a default response handler in Postman tests?
You want to write a Postman test script that handles multiple response codes (200, 201, 404) with specific assertions and also has a default assertion for any other response code. Which script correctly implements this?
A
pm.test('Response code check', () => {
  pm.expect(pm.response.code).to.be.oneOf([200,201,404]);
});
B
pm.test('Response code check', () => {
  switch(pm.response.code) {
    case 200:
      pm.expect(pm.response.code).to.equal(200);
      break;
    case 201:
      pm.expect(pm.response.code).to.equal(201);
      break;
    case 404:
      pm.expect(pm.response.code).to.equal(404);
      break;
    default:
      pm.expect.fail(`Unexpected status code: ${pm.response.code}`);
  }
});
C
pm.test('Response code check', () => {
  if ([200,201,404].includes(pm.response.code)) {
    pm.expect(pm.response.code).to.be.oneOf([200,201,404]);
  }
});
D
pm.test('Response code check', () => {
  if (pm.response.code === 200) {
    pm.expect(pm.response.code).to.equal(200);
  } else if (pm.response.code === 201) {
    pm.expect(pm.response.code).to.equal(201);
  } else if (pm.response.code === 404) {
    pm.expect(pm.response.code).to.equal(404);
  } else {
    pm.expect(pm.response.code).to.be.oneOf([200,201,404]);
  }
});
Attempts:
2 left
💡 Hint
Look for a default case that fails the test explicitly.

Practice

(1/5)
1. What is the main purpose of using default and conditional responses in Postman tests?
easy
A. To send multiple requests at the same time
B. To change the API endpoint dynamically
C. To check different API responses in one test script
D. To automatically generate API documentation

Solution

  1. Step 1: Understand the role of conditional responses

    Conditional responses allow testing different API replies based on conditions in one place.
  2. Step 2: Identify the main purpose

    This helps verify various outcomes without writing separate tests for each response.
  3. Final Answer:

    To check different API responses in one test script -> Option C
  4. Quick Check:

    Default and conditional responses = test multiple replies [OK]
Hint: Think: one test script, many response checks [OK]
Common Mistakes:
  • Confusing response testing with sending requests
  • Assuming it changes API endpoints
  • Mixing testing with documentation generation
2. Which of the following is the correct syntax to write a conditional test in Postman to check if the response status is 200?
easy
A. pm.test('Status is 200', () => { if (pm.response.code === 200) { pm.expect(true).to.be.true; } });
B. pm.test('Status is 200', () => { pm.expect(pm.response.code).to.equal(200); });
C. pm.test('Status is 200', () => pm.response.code == 200);
D. pm.test('Status is 200', () => { if (pm.response.status == 200) pm.expect(true); });

Solution

  1. Step 1: Review Postman test syntax

    Postman uses pm.test with a callback function and pm.expect for assertions.
  2. Step 2: Identify correct assertion for status code

    pm.expect(pm.response.code).to.equal(200) correctly asserts status code equals 200.
  3. Final Answer:

    pm.test('Status is 200', () => { pm.expect(pm.response.code).to.equal(200); }); -> Option B
  4. Quick Check:

    pm.expect with .to.equal(200) = correct syntax [OK]
Hint: Use pm.expect with .to.equal for exact value checks [OK]
Common Mistakes:
  • Using '==' instead of .to.equal() in assertions
  • Checking pm.response.status instead of pm.response.code
  • Not calling pm.expect properly inside pm.test
3. Given this Postman test code, what will be the test result if the response status is 404?
pm.test('Check response', () => {
  if (pm.response.code === 200) {
    pm.expect(pm.response.json().success).to.be.true;
  } else {
    pm.expect(pm.response.code).to.equal(404);
  }
});
medium
A. Test passes because status is 404 and matches else condition
B. Test fails because success property is missing
C. Test throws an error due to missing JSON body
D. Test passes only if success is true

Solution

  1. Step 1: Analyze the if-else condition with status 404

    Status 404 triggers the else block which asserts pm.response.code equals 404.
  2. Step 2: Check assertion in else block

    pm.expect(pm.response.code).to.equal(404) will pass since status is 404.
  3. Final Answer:

    Test passes because status is 404 and matches else condition -> Option A
  4. Quick Check:

    404 status triggers else assertion = pass [OK]
Hint: Match status code to correct if/else branch [OK]
Common Mistakes:
  • Assuming test fails due to missing success property
  • Thinking JSON parsing fails on 404
  • Ignoring else block assertions
4. Identify the error in this Postman test script that checks for a 201 status or a 400 error:
pm.test('Status check', () => {
  if (pm.response.code = 201) {
    pm.expect(pm.response.code).to.equal(201);
  } else if (pm.response.code = 400) {
    pm.expect(pm.response.json().error).to.exist;
  }
});
medium
A. No else block to handle other status codes
B. Missing pm.expect in the first if block
C. Incorrect JSON parsing method
D. Using assignment '=' instead of comparison '===' in if conditions

Solution

  1. Step 1: Check if condition syntax

    The code uses '=' which assigns value instead of '===' for comparison.
  2. Step 2: Understand impact of assignment in conditions

    Assignment always returns true, causing logic errors and wrong test behavior.
  3. Final Answer:

    Using assignment '=' instead of comparison '===' in if conditions -> Option D
  4. Quick Check:

    Use '===' for comparisons, not '=' [OK]
Hint: Use '===' for comparisons, '=' is assignment [OK]
Common Mistakes:
  • Confusing '=' with '==' or '==='
  • Ignoring that assignment returns a value
  • Not validating all possible status codes
5. You want to write a Postman test that checks if the response status is 200 and the JSON body has a 'data' field, but if the status is 404, it should check for an 'error' message. Which code snippet correctly implements this conditional response test?
hard
A. pm.test('Conditional response test', () => { if (pm.response.code === 200) { pm.expect(pm.response.json().data).to.exist; } else if (pm.response.code === 404) { pm.expect(pm.response.json().error).to.exist; } else { pm.expect.fail('Unexpected status code'); } });
B. pm.test('Conditional response test', () => { pm.expect(pm.response.code === 200 && pm.response.json().data).to.exist; pm.expect(pm.response.code === 404 && pm.response.json().error).to.exist; });
C. pm.test('Conditional response test', () => { if (pm.response.code = 200) { pm.expect(pm.response.json().data).to.exist; } else if (pm.response.code = 404) { pm.expect(pm.response.json().error).to.exist; } });
D. pm.test('Conditional response test', () => { pm.expect(pm.response.json().data || pm.response.json().error).to.exist; });

Solution

  1. Step 1: Check correct use of comparison operators

    pm.test('Conditional response test', () => { if (pm.response.code === 200) { pm.expect(pm.response.json().data).to.exist; } else if (pm.response.code === 404) { pm.expect(pm.response.json().error).to.exist; } else { pm.expect.fail('Unexpected status code'); } }); uses '===' for comparisons correctly. Using '=' causes assignment instead of comparison.
  2. Step 2: Verify conditional logic and assertions

    The code checks 'data' field existence for status 200, 'error' for 404, and explicitly fails for unexpected codes using pm.expect.fail().
  3. Step 3: Evaluate other options

    One option incorrectly combines status and field checks in single pm.expect statements, causing failures. Another does not condition on status codes at all.
  4. Final Answer:

    pm.test('Conditional response test', () => { if (pm.response.code === 200) { pm.expect(pm.response.json().data).to.exist; } else if (pm.response.code === 404) { pm.expect(pm.response.json().error).to.exist; } else { pm.expect.fail('Unexpected status code'); } }); -> Option A
  5. Quick Check:

    Use if-else with '===' and proper assertions [OK]
Hint: Use if-else with '===' and separate assertions [OK]
Common Mistakes:
  • Using '=' instead of '===' in conditions
  • Combining conditions inside one pm.expect
  • Not handling unexpected status codes