The Tests tab in Postman lets you write checks to see if your API works as expected. pm.test() helps you write these checks easily.
Tests tab and pm.test() in Postman
Start learning this pattern below
Jump into concepts and practice - no test required
pm.test("Test name", function () {
pm.expect(actual).to.eql(expected);
});pm.test() takes two parts: a name for the test and a function with the check.
Inside the function, use pm.expect() to compare actual and expected results.
pm.test("Status code is 200", function () { pm.response.to.have.status(200); });
name field equal to "Alice".pm.test("Response has user name", function () { const jsonData = pm.response.json(); pm.expect(jsonData.name).to.eql("Alice"); });
Content-Type is exactly "application/json".pm.test("Content-Type is JSON", function () { pm.response.to.have.header("Content-Type", "application/json"); });
This script runs two tests: one checks the status code is 200, the other checks the user ID in the response JSON is 123.
pm.test("Status code is 200", function () { pm.response.to.have.status(200); }); pm.test("Response has correct user ID", function () { const jsonData = pm.response.json(); pm.expect(jsonData.id).to.eql(123); });
Always give your tests clear, descriptive names so you know what they check.
If a test fails, Postman shows which test and why, helping you fix issues quickly.
You can write many tests in the Tests tab to cover different parts of the response.
The Tests tab lets you write checks to verify API responses automatically.
pm.test() defines a test with a name and a check function.
Use pm.expect() inside pm.test() to compare actual and expected values.
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
