What if you never had to guess if your API worked right after sending a request?
Why Tests tab and pm.test() in Postman? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you manually check each API response by reading through long JSON data every time you send a request in Postman.
You try to remember if the status code was 200 or if the response had the right fields.
This manual checking is slow and tiring.
You can easily miss errors or forget to check important details.
It's hard to keep track of what passed or failed after many requests.
The Tests tab with pm.test() lets you write small checks that run automatically after each request.
It quickly tells you if your API works as expected, saving time and avoiding mistakes.
Send request -> Read response -> Remember if status is 200 -> Check fields manually
pm.test('Status is 200', () => { pm.response.to.have.status(200); });
You can instantly see which API tests pass or fail, making your testing faster and more reliable.
When testing a login API, pm.test() can automatically check if the response contains a token and a success message every time you send the request.
Manual checking is slow and error-prone.
pm.test() automates response checks in Postman.
This makes API testing faster, clearer, and less stressful.
Practice
pm.test() function in Postman's Tests tab?Solution
Step 1: Understand the role of pm.test()
Thepm.test()function is used to define a test with a descriptive name and a function that contains checks or assertions.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.Final Answer:
To define a named test and run assertions on the API response -> Option AQuick Check:
pm.test() defines tests = A [OK]
- Confusing pm.test() with sending requests
- Thinking pm.test() formats data
- Assuming pm.test() creates collections
Solution
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.Step 2: Check assertion method for status code
Usingpm.response.to.have.status(200)is the proper way to assert status code 200.Final Answer:
pm.test('Status code is 200', () => pm.response.to.have.status(200)); -> Option BQuick Check:
Correct syntax uses arrow function and .to.have.status() [OK]
- Passing boolean instead of function to pm.test()
- Using assignment '=' instead of comparison '=='
- Omitting parentheses in function declaration
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}?Solution
Step 1: Understand the test assertion
The test checks ifjsonData.userIdequals 1 usingpm.expect().to.eql(1).Step 2: Compare actual response value
The response hasuserIdas 2, which does not equal 1, so the assertion fails.Final Answer:
The test will fail because userId is not 1 -> Option AQuick Check:
Assertion fails if values differ = B [OK]
- Assuming test passes if key exists
- Confusing eql() with assignment
- Thinking test skips on mismatch
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?
Solution
Step 1: Verify property name and assertion
pm.response.responseTimeis the correct property for response time in milliseconds, andto.be.below()is valid syntax.Step 2: Consider why test fails despite correct syntax
IfresponseTimeis undefined or not a number, the assertion will fail even if the actual response time is low.Final Answer:
The property pm.response.responseTime is correct, but the test fails if responseTime is undefined or not a number -> Option CQuick Check:
Undefined or wrong type causes assertion failure = A [OK]
- Assuming wrong assertion method causes failure
- Using incorrect property names
- Missing parentheses in arrow function
items and that each item has a price greater than 0. Which test code correctly implements this?Solution
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 usespm.expect(jsonData.items).to.be.an('array')andto.be.greaterThan(0)to verify the array is non-empty.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)); }); usespm.expect(item.price).to.be.greaterThan(0)inside aforEachloop, which is valid and clear.Step 3: Compare with other options
Options A and B use slightly different syntax but A usesto.be.above(0)which is valid but less common thanto.be.greaterThan(0). B usesevery()which is valid but chainingto.be.an('array').and.not.emptyis not standard Chai syntax in Postman. C has incorrect assertions missing function calls.Final Answer:
Option D correctly implements all checks with valid syntax -> Option DQuick Check:
Use .to.be.an('array') and .to.be.greaterThan() for checks [OK]
- Using incorrect assertion chaining syntax
- Missing function calls in pm.expect()
- Not checking array length before iterating
