Bird
Raised Fist0
Postmantesting~10 mins

Status code assertion in Postman - Interactive Code Practice

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
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if the response status code is 200.

Postman
pm.test("Status code is 200", function () {
    pm.response.to.have.status([1]);
});
Drag options to blanks, or click blank then click option'
A201
B404
C500
D200
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong status code like 404 or 500.
Forgetting to put the status code as a number.
2fill in blank
medium

Complete the code to assert the response status code is 404.

Postman
pm.test("Status code is 404", function () {
    pm.response.to.have.status([1]);
});
Drag options to blanks, or click blank then click option'
A404
B200
C500
D301
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing 404 with 200 or 500.
Using string instead of number for status code.
3fill in blank
hard

Fix the error in the code to correctly assert status code 201.

Postman
pm.test("Status code is 201", function () {
    pm.response.to.have.status([1]);
});
Drag options to blanks, or click blank then click option'
A202
B201
C"201"
D200
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes around the status code number.
Using a wrong status code like 200 or 202.
4fill in blank
hard

Fill both blanks to assert the status code is greater than or equal to 200.

Postman
pm.test("Status code is success range", function () {
    pm.expect(pm.response.code).to.be.[1]([2]);
});
Drag options to blanks, or click blank then click option'
Aat.least
Bat.most
Cequal
D200
Attempts:
3 left
💡 Hint
Common Mistakes
Using equal instead of range checks.
Using wrong comparison methods like at.most with 200.
5fill in blank
hard

Fill all three blanks to assert the status code is between 200 and 299 inclusive.

Postman
pm.test("Status code is 2xx", function () {
    pm.expect(pm.response.code).to.be.[1]([2]);
    pm.expect(pm.response.code).to.be.[3](299);
});
Drag options to blanks, or click blank then click option'
Aat.least
B200
Cat.most
Dequal
Attempts:
3 left
💡 Hint
Common Mistakes
Using equal instead of range checks.
Mixing up at.least and at.most.

Practice

(1/5)
1. What does the status code 200 usually mean in a Postman response?
easy
A. The request was successful
B. The server could not find the requested resource
C. There was a server error
D. The request was unauthorized

Solution

  1. Step 1: Understand HTTP status codes

    Status code 200 means the request was processed successfully by the server.
  2. Step 2: Match code to meaning

    200 is the standard code for success, unlike 404 (not found) or 500 (server error).
  3. Final Answer:

    The request was successful -> Option A
  4. Quick Check:

    Status 200 = Success [OK]
Hint: 200 means success, always check for it first [OK]
Common Mistakes:
  • Confusing 200 with error codes like 404 or 500
  • Thinking 200 means unauthorized access
  • Assuming 200 means redirect
2. Which of the following is the correct syntax to assert a status code of 404 in a Postman test script?
easy
A. pm.response.assert.status(404);
B. pm.response.to.have.status(404);
C. pm.response.status(404);
D. pm.test.status(404);

Solution

  1. Step 1: Recall Postman assertion syntax

    The correct way to check status code is using pm.response.to.have.status(code).
  2. Step 2: Verify each option

    Only pm.response.to.have.status(404); matches the correct syntax exactly.
  3. Final Answer:

    pm.response.to.have.status(404); -> Option B
  4. Quick Check:

    Use pm.response.to.have.status() [OK]
Hint: Use pm.response.to.have.status(code) for status checks [OK]
Common Mistakes:
  • Omitting 'to.have' in the assertion
  • Using pm.response.status() which is invalid
  • Trying pm.test.status() which does not exist
3. Given this Postman test code:
pm.test('Check status', () => {
  pm.response.to.have.status(201);
});

What will be the test result if the server responds with status code 200?
medium
A. Test will pass
B. Test will be skipped
C. Test will error out
D. Test will fail

Solution

  1. Step 1: Understand the assertion

    The test expects status code 201 (Created) exactly.
  2. Step 2: Compare actual response code

    The server returned 200 (OK), which does not match 201, so assertion fails.
  3. Final Answer:

    Test will fail -> Option D
  4. Quick Check:

    Expected 201 but got 200 = Fail [OK]
Hint: Exact status code must match to pass assertion [OK]
Common Mistakes:
  • Assuming 200 and 201 are interchangeable
  • Thinking test errors instead of fails
  • Believing test skips on mismatch
4. You wrote this Postman test:
pm.test('Status is 200', () => {
  pm.response.to.have.status = 200;
});

What is wrong with this test?
medium
A. Status code 200 is invalid
B. Missing semicolon at the end
C. Incorrect use of assignment instead of function call
D. pm.test syntax is wrong

Solution

  1. Step 1: Check assertion syntax

    The code uses = which assigns a value instead of calling status(200) as a function.
  2. Step 2: Identify correct usage

    The correct syntax is pm.response.to.have.status(200); to assert status code.
  3. Final Answer:

    Incorrect use of assignment instead of function call -> Option C
  4. Quick Check:

    Use parentheses for function calls, not assignment [OK]
Hint: Use parentheses () to call status function, not = [OK]
Common Mistakes:
  • Using = instead of calling status()
  • Ignoring syntax errors thinking test runs
  • Confusing semicolon importance
5. You want to write a Postman test that passes only if the response status code is either 200 or 201. Which code snippet correctly asserts this?
hard
A. pm.test('Status is 200 or 201', () => { pm.expect(pm.response.code === 200 || pm.response.code === 201).to.be.true; });
B. pm.test('Status is 200 or 201', () => { pm.response.to.have.status(200 || 201); });
C. pm.test('Status is 200 or 201', () => { pm.response.to.have.status(200) || pm.response.to.have.status(201); });
D. pm.test('Status is 200 or 201', () => { pm.response.to.have.status([200, 201]); });

Solution

  1. Step 1: Understand how to check multiple status codes

    Postman does not support passing multiple codes directly to status().
  2. Step 2: Use logical OR with pm.expect

    pm.test('Status is 200 or 201', () => { pm.expect(pm.response.code === 200 || pm.response.code === 201).to.be.true; }); uses pm.expect with a boolean expression checking if code is 200 or 201, which is correct.
  3. Final Answer:

    pm.test('Status is 200 or 201', () => { pm.expect(pm.response.code === 200 || pm.response.code === 201).to.be.true; }); -> Option A
  4. Quick Check:

    Use pm.expect with OR condition for multiple codes [OK]
Hint: Use pm.expect with OR to check multiple status codes [OK]
Common Mistakes:
  • Passing multiple codes directly to status()
  • Using logical OR outside pm.expect
  • Assuming status() accepts arrays