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
Recall & Review
beginner
What is the purpose of the Tests tab in Postman?
The Tests tab in Postman is used to write scripts that run after a request is sent. These scripts check if the response meets expected conditions, helping verify the API works correctly.
Click to reveal answer
beginner
What does the pm.test() function do in Postman?
The pm.test() function defines a test with a name and a function containing assertions. It runs the test and reports if it passed or failed based on the assertions inside.
Click to reveal answer
beginner
Write a simple pm.test() example that checks if the response status code is 200.
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
Click to reveal answer
beginner
Why is it important to name your tests clearly in pm.test()?
Clear test names help you understand what each test checks when you see the test results. It makes debugging easier if a test fails.
Click to reveal answer
intermediate
How can you check if a JSON response contains a specific key using pm.test()?
Use pm.test() with pm.response.json() to parse the response and then check the key. Example:
pm.test("Response has userId", function () {
const jsonData = pm.response.json();
pm.expect(jsonData).to.have.property('userId');
});
Click to reveal answer
What does pm.test() require as arguments?
AOnly a function with assertions
BOnly a test name
CA test name and a function with assertions
DA request URL and headers
✗ Incorrect
pm.test() needs a descriptive test name and a function that contains the assertions to check the response.
Where do you write pm.test() scripts in Postman?
AIn the Tests tab after sending a request
BIn the Body tab before sending a request
CIn the Headers tab
DIn the Authorization tab
✗ Incorrect
The Tests tab is where you write scripts that run after the request completes.
Which assertion checks if the response status code is 200?
Apm.response.to.have.status(200);
Bpm.response.status(200);
Cpm.response.code(200);
Dpm.response.check(200);
✗ Incorrect
The correct syntax to assert status code 200 is pm.response.to.have.status(200);
What happens if an assertion inside pm.test() fails?
AThe test passes anyway
BThe request is resent automatically
CPostman stops running all tests
DThe test fails and is reported in the test results
✗ Incorrect
If an assertion fails, the test is marked as failed and shown in the test results.
How do you access the JSON response body inside pm.test()?
AUsing pm.response.text()
BUsing pm.response.json()
CUsing pm.response.body()
DUsing pm.request.json()
✗ Incorrect
pm.response.json() parses the response body as JSON for assertions.
Explain how to write a test in Postman using pm.test() to check if the response status code is 200.
Think about naming the test and the assertion syntax.
You got /3 concepts.
Describe the role of the Tests tab in Postman and how pm.test() fits into it.
Consider when tests run and what they do.
You got /4 concepts.
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
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.
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 A
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
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
Using pm.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 B
Quick Check:
Correct syntax uses arrow function and .to.have.status() [OK]
Hint: Use arrow function and .to.have.status() for status checks [OK]
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
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.
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.
Final Answer:
The property pm.response.responseTime is correct, but the test fails if responseTime is undefined or not a number -> Option C
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;
});
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
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.
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.
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.
Final Answer:
Option D correctly implements all checks with valid syntax -> Option D
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]