Bird
Raised Fist0
Postmantesting~20 mins

Tests tab and pm.test() 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
🎖️
Postman Tests Master
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?
Consider this Postman test script in the Tests tab:
pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});
pm.test("Response time is less than 500ms", function () {
    pm.expect(pm.response.responseTime).to.be.below(500);
});

Assuming the response status is 200 and response time is 450ms, what will be the test execution result?
Postman
pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});
pm.test("Response time is less than 500ms", function () {
    pm.expect(pm.response.responseTime).to.be.below(500);
});
ABoth tests pass
BFirst test fails, second test passes
CFirst test passes, second test fails
DBoth tests fail
Attempts:
2 left
💡 Hint
Check the status code and response time conditions carefully.
assertion
intermediate
2:00remaining
Which pm.test assertion correctly checks if JSON response has a key 'userId'?
You want to write a test in Postman to verify that the JSON response body contains the key 'userId'. Which pm.test assertion below will correctly do this?
Apm.test("Response has userId", () => { pm.expect(pm.response.json().userId).to.exist; });
Bpm.test("Response has userId", () => { pm.expect(pm.response.json()).to.have.property('userId'); });
Cpm.test("Response has userId", () => { pm.expect(pm.response.json().has('userId')).to.be.true; });
Dpm.test("Response has userId", () => { pm.expect(pm.response.json().includes('userId')).to.be.true; });
Attempts:
2 left
💡 Hint
Check which method correctly tests for a property in an object.
🔧 Debug
advanced
2:00remaining
Why does this pm.test fail with a TypeError?
Look at this Postman test code:
pm.test("Check user name", function() {
    let jsonData = pm.response.json;
    pm.expect(jsonData.name).to.eql("Alice");
});

What causes the TypeError during test execution?
Postman
pm.test("Check user name", function() {
    let jsonData = pm.response.json;
    pm.expect(jsonData.name).to.eql("Alice");
});
AThe test function is missing a return statement
Bpm.expect is not defined in the Tests tab
Cpm.response.json is a function and needs to be called as pm.response.json()
DThe property 'name' does not exist in the response
Attempts:
2 left
💡 Hint
Check how to correctly get JSON data from the response.
🧠 Conceptual
advanced
2:00remaining
What is the purpose of pm.test() in Postman?
Select the best description of what pm.test() does in Postman test scripts.
AIt sends the HTTP request to the server and waits for the response.
BIt automatically retries the request if the test fails.
CIt logs the response body to the Postman console for debugging.
DIt defines a test case with a name and a function containing assertions to validate the response.
Attempts:
2 left
💡 Hint
Think about how tests are structured in Postman.
framework
expert
2:00remaining
Which option correctly waits for a response before running pm.test assertions in Postman?
You want to write a test script that runs assertions only after the response is fully received. Which code snippet correctly ensures this in Postman?
Apm.sendRequest(request, function (err, res) { pm.test("Status is 200", () => { pm.expect(res).to.have.property('code', 200); }); });
Bpm.test("Status is 200", () => { pm.expect(pm.response.code).to.equal(200); });
Cpm.response.on('complete', () => { pm.test("Status is 200", () => { pm.expect(pm.response.code).to.equal(200); }); });
Dpm.waitForResponse(() => { pm.test("Status is 200", () => { pm.expect(pm.response.code).to.equal(200); }); });
Attempts:
2 left
💡 Hint
Check which method uses a callback after the request completes.

Practice

(1/5)
1. What is the main purpose of the pm.test() function in Postman's Tests tab?
easy
A. To define a named test and run assertions on the API response
B. To send an API request to the server
C. To format the API response data
D. To create a new collection in Postman

Solution

  1. Step 1: Understand the role of pm.test()

    The pm.test() function is used to define a test with a descriptive name and a function that contains checks or assertions.
  2. Step 2: Identify what pm.test() does in the Tests tab

    It runs the test function to verify if the API response meets expected conditions, helping automate validation.
  3. Final Answer:

    To define a named test and run assertions on the API response -> Option A
  4. Quick Check:

    pm.test() defines tests = A [OK]
Hint: pm.test() names and runs checks on responses [OK]
Common Mistakes:
  • Confusing pm.test() with sending requests
  • Thinking pm.test() formats data
  • Assuming pm.test() creates collections
2. Which of the following is the correct syntax to write a test in Postman that checks if the response status code is 200?
easy
A. pm.test('Status code is 200', function { pm.response.status == 200 });
B. pm.test('Status code is 200', () => pm.response.to.have.status(200));
C. pm.test('Status code is 200', pm.response.status === 200);
D. pm.test('Status code is 200', () => pm.response.status = 200);

Solution

  1. Step 1: Review correct pm.test() syntax

    The correct syntax uses a test name string and a function with assertions inside, like an arrow function.
  2. Step 2: Check assertion method for status code

    Using pm.response.to.have.status(200) is the proper way to assert status code 200.
  3. Final Answer:

    pm.test('Status code is 200', () => pm.response.to.have.status(200)); -> Option B
  4. Quick Check:

    Correct syntax uses arrow function and .to.have.status() [OK]
Hint: Use arrow function and .to.have.status() for status checks [OK]
Common Mistakes:
  • Passing boolean instead of function to pm.test()
  • Using assignment '=' instead of comparison '=='
  • Omitting parentheses in function declaration
3. Consider this test code in Postman:
pm.test('Response has userId 1', () => {
  const jsonData = pm.response.json();
  pm.expect(jsonData.userId).to.eql(1);
});

What will happen if the API response JSON is {"userId": 2}?
medium
A. The test will fail because userId is not 1
B. The test will pass because userId exists
C. The test will throw a syntax error
D. The test will be skipped automatically

Solution

  1. Step 1: Understand the test assertion

    The test checks if jsonData.userId equals 1 using pm.expect().to.eql(1).
  2. Step 2: Compare actual response value

    The response has userId as 2, which does not equal 1, so the assertion fails.
  3. Final Answer:

    The test will fail because userId is not 1 -> Option A
  4. Quick Check:

    Assertion fails if values differ = B [OK]
Hint: pm.expect() fails if actual ≠ expected [OK]
Common Mistakes:
  • Assuming test passes if key exists
  • Confusing eql() with assignment
  • Thinking test skips on mismatch
4. You wrote this test in Postman:
pm.test('Check response time', () => {
  pm.expect(pm.response.responseTime).to.be.below(200);
});

But the test always fails even when response time is below 200ms. What is the likely error?
medium
A. The property pm.response.responseTime is correct, but the test function is missing parentheses
B. Incorrect property name; should be pm.response.response_time
C. The property pm.response.responseTime is correct, but the test fails if responseTime is undefined or not a number
D. Using to.be.below instead of to.be.lessThan

Solution

  1. Step 1: Verify property name and assertion

    pm.response.responseTime is the correct property for response time in milliseconds, and to.be.below() is valid syntax.
  2. Step 2: Consider why test fails despite correct syntax

    If responseTime is undefined or not a number, the assertion will fail even if the actual response time is low.
  3. Final Answer:

    The property pm.response.responseTime is correct, but the test fails if responseTime is undefined or not a number -> Option C
  4. Quick Check:

    Undefined or wrong type causes assertion failure = A [OK]
Hint: Check property exists and is number before asserting [OK]
Common Mistakes:
  • Assuming wrong assertion method causes failure
  • Using incorrect property names
  • Missing parentheses in arrow function
5. You want to write a Postman test that checks if the response JSON contains a non-empty array called items and that each item has a price greater than 0. Which test code correctly implements this?
hard
A. pm.test('Items array and prices', () => { const jsonData = pm.response.json(); pm.expect(Array.isArray(jsonData.items)).to.be.true; pm.expect(jsonData.items.length).to.be.above(0); jsonData.items.forEach(item => pm.expect(item.price).to.be.above(0)); });
B. pm.test('Items array and prices', () => { const jsonData = pm.response.json(); pm.expect(jsonData.items).to.be.an('array').and.not.empty; pm.expect(jsonData.items.every(item => item.price > 0)).to.be.true; });
C. pm.test('Items array and prices', () => { const jsonData = pm.response.json(); pm.expect(jsonData.items).to.exist; pm.expect(jsonData.items.length > 0); jsonData.items.forEach(item => pm.expect(item.price > 0)); });
D. pm.test('Items array and prices', () => { const jsonData = pm.response.json(); pm.expect(jsonData.items).to.be.an('array'); pm.expect(jsonData.items.length).to.be.greaterThan(0); jsonData.items.forEach(item => pm.expect(item.price).to.be.greaterThan(0)); });

Solution

  1. Step 1: Check array existence and length

    pm.test('Items array and prices', () => { const jsonData = pm.response.json(); pm.expect(jsonData.items).to.be.an('array'); pm.expect(jsonData.items.length).to.be.greaterThan(0); jsonData.items.forEach(item => pm.expect(item.price).to.be.greaterThan(0)); }); correctly uses pm.expect(jsonData.items).to.be.an('array') and to.be.greaterThan(0) to verify the array is non-empty.
  2. Step 2: Verify each item's price check

    pm.test('Items array and prices', () => { const jsonData = pm.response.json(); pm.expect(jsonData.items).to.be.an('array'); pm.expect(jsonData.items.length).to.be.greaterThan(0); jsonData.items.forEach(item => pm.expect(item.price).to.be.greaterThan(0)); }); uses pm.expect(item.price).to.be.greaterThan(0) inside a forEach loop, which is valid and clear.
  3. Step 3: Compare with other options

    Options A and B use slightly different syntax but A uses to.be.above(0) which is valid but less common than to.be.greaterThan(0). B uses every() which is valid but chaining to.be.an('array').and.not.empty is not standard Chai syntax in Postman. C has incorrect assertions missing function calls.
  4. Final Answer:

    Option D correctly implements all checks with valid syntax -> Option D
  5. Quick Check:

    Use .to.be.an('array') and .to.be.greaterThan() for checks [OK]
Hint: Use .to.be.an('array') and .to.be.greaterThan() for clear checks [OK]
Common Mistakes:
  • Using incorrect assertion chaining syntax
  • Missing function calls in pm.expect()
  • Not checking array length before iterating