Automated assertions check if the response from a server is correct without needing a person to look at it. This saves time and avoids mistakes.
Why automated assertions validate responses in Postman
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Postman
pm.test('Test description', function () { pm.response.to.have.status(200); pm.expect(pm.response.json().key).to.eql('expected value'); });
pm.test defines a test with a description.
pm.response accesses the response to check its status or body.
Examples
Postman
pm.test('Status is 200', function () { pm.response.to.have.status(200); });
Postman
pm.test('Response has user name', function () { const jsonData = pm.response.json(); pm.expect(jsonData.name).to.eql('Alice'); });
Postman
pm.test('Response time is less than 500ms', function () { pm.expect(pm.response.responseTime).to.be.below(500); });
Sample Program
This Postman test script checks two things: the server responded with status 200, and the response JSON has a message 'Success'.
Postman
pm.test('Status code is 200', function () { pm.response.to.have.status(200); }); pm.test('Response contains success message', function () { const jsonData = pm.response.json(); pm.expect(jsonData.message).to.eql('Success'); });
Important Notes
Always write clear test descriptions so you know what each test checks.
Use assertions that match what your API should return to catch real problems.
Automated assertions help you test faster and more reliably than manual checks.
Summary
Automated assertions check if responses are correct without manual work.
They save time and catch errors early.
Postman uses simple code to write these tests.
Practice
1. Why do automated assertions help when testing API responses in Postman?
easy
Solution
Step 1: Understand the role of automated assertions
Automated assertions are used to verify if the API response data is correct without needing a person to check it manually.Step 2: Identify what automated assertions do in Postman
They run tests automatically after a request and confirm if the response meets the expected conditions.Final Answer:
They automatically check if the response matches expected results without manual review. -> Option DQuick Check:
Automated assertions = automatic response checks [OK]
Hint: Automated assertions save manual checking time [OK]
Common Mistakes:
- Thinking assertions speed up the API itself
- Believing assertions fix errors automatically
- Confusing assertions with deleting data
2. Which of the following is the correct syntax to assert that the response status code is 200 in Postman test script?
easy
Solution
Step 1: Recall Postman test syntax
Postman uses pm.test() with a callback function to run assertions.Step 2: Identify the correct assertion method
The correct way to check status code is pm.response.to.have.status(200) inside the callback.Final Answer:
pm.test('Status code is 200', () => pm.response.to.have.status(200)); -> Option BQuick Check:
pm.test + arrow function + .to.have.status(200) [OK]
Hint: Use pm.test with arrow function and .to.have.status() [OK]
Common Mistakes:
- Omitting the arrow function in pm.test
- Using incorrect assertion methods like assertStatus
- Passing boolean directly instead of function
3. Given this Postman test code:
What will happen if the API response is
pm.test('Check user name', () => {
const jsonData = pm.response.json();
pm.expect(jsonData.name).to.eql('Alice');
});What will happen if the API response is
{"name": "Bob"}?medium
Solution
Step 1: Understand the assertion in the test
The test expects the JSON response's 'name' field to equal 'Alice'.Step 2: Compare expected and actual response
The actual response has 'name' as 'Bob', which does not match 'Alice', so the assertion fails.Final Answer:
The test will fail because the name is not 'Alice'. -> Option CQuick Check:
Expected 'Alice' but got 'Bob' = fail [OK]
Hint: Check if expected value matches actual response exactly [OK]
Common Mistakes:
- Assuming test passes if field exists regardless of value
- Thinking syntax error occurs for value mismatch
- Believing tests skip on assertion failure
4. You wrote this Postman test:
But the test always fails even when the response contains
pm.test('Response has userId', () => {
pm.expect(pm.response.json().userId).to.exist;
});But the test always fails even when the response contains
{"userId": 123}. What is the likely problem?medium
Solution
Step 1: Check how pm.response.json() works
pm.response.json() parses the response body as JSON. If the response is not valid JSON, it will throw an error or return undefined.Step 2: Consider response format causing test failure
If the response is a plain string or invalid JSON, json() fails, so userId is undefined and assertion fails.Final Answer:
The response might be a string, not JSON, causing json() to fail. -> Option AQuick Check:
Invalid JSON response breaks json() = test fail [OK]
Hint: Ensure response is valid JSON before using json() [OK]
Common Mistakes:
- Thinking missing semicolon causes test failure
- Using wrong assertion syntax like .to.be.exist
- Assuming json() always succeeds regardless of response
5. You want to write an automated assertion in Postman that checks if the response JSON array has at least one object with
status equal to "active". Which test code correctly validates this?hard
Solution
Step 1: Understand the goal of the assertion
The test must confirm at least one object in the array has status exactly 'active'.Step 2: Analyze each option's correctness
pm.test('Has active status', () => { const data = pm.response.json(); pm.expect(data.some(item => item.status === 'active')).to.be.true; }); uses data.some() which returns true if any item matches the condition, then asserts true correctly.
pm.test('Has active status', () => { const data = pm.response.json(); pm.expect(data.filter(item => item.status === 'active')).to.exist; }); uses filter() but checks .to.exist which is always true for an array, not a boolean.
pm.test('Has active status', () => { const data = pm.response.json(); pm.expect(data.find(item => item.status = 'active')).to.be.true; }); uses assignment (=) instead of comparison (===), causing a bug.
pm.test('Has active status', () => { const data = pm.response.json(); pm.expect(data.includes('active')).to.be.true; }); uses includes('active') which checks for string presence, not object property.Final Answer:
pm.test('Has active status', () => { const data = pm.response.json(); pm.expect(data.some(item => item.status === 'active')).to.be.true; }); -> Option AQuick Check:
Use some() + strict equality + expect true [OK]
Hint: Use some() to check condition on array items [OK]
Common Mistakes:
- Using assignment (=) instead of comparison (===)
- Checking filter() result existence instead of boolean
- Using includes() on array of objects incorrectly
