Response body assertions in Postman - Build an Automation Script
pm.test('Status code is 200', () => { pm.response.to.have.status(200); }); pm.test('Response body has expected user data', () => { const jsonData = pm.response.json(); pm.expect(jsonData).to.have.all.keys('id', 'name', 'email'); pm.expect(jsonData.id).to.eql(123); pm.expect(jsonData.name).to.eql('John Doe'); pm.expect(jsonData.email).to.eql('john.doe@example.com'); });
The first test checks that the response status code is exactly 200 using pm.response.to.have.status(200). This ensures the request was successful.
The second test parses the response body JSON with pm.response.json(). It then asserts the JSON object has all the keys id, name, and email using to.have.all.keys. This confirms the expected data structure.
Finally, it checks each key's value matches the expected data using strict equality to.eql(). This verifies the correctness of the response content.
Each assertion has a clear message to help identify failures during test runs.
Now add data-driven testing with 3 different user IDs and expected user data
Practice
pm.response.json() do in Postman tests?Solution
Step 1: Understand the purpose of
This function reads the response body and converts it into a JSON object for easy access.pm.response.json()Step 2: Compare with other options
Sending requests, clearing body, or validating status are different functions, notpm.response.json().Final Answer:
It parses the response body as a JSON object. -> Option AQuick Check:
Parsing response body = A [OK]
- Confusing json() with sending requests
- Thinking json() clears data
- Mixing response body parsing with status code checks
status with value success in Postman?Solution
Step 1: Identify correct assertion syntax in Postman
Postman usespm.expect()with Chai assertion style, sopm.expect(pm.response.json().status).to.eql('success');is correct.Step 2: Check other options for errors
pm.response.json().status == 'success'; lacks assertion, C uses wrong object, D uses incorrect method.Final Answer:
pm.expect(pm.response.json().status).to.eql('success'); -> Option CQuick Check:
Use pm.expect() with to.eql() for value check [OK]
- Using == instead of pm.expect() for assertions
- Referencing response.status instead of response.json().status
- Using pm.assert() which is not a Postman function
{"user":{"id":5,"name":"Alice"}} What will this test output?const jsonData = pm.response.json();
pm.test("User ID is 5", () => {
pm.expect(jsonData.user.id).to.equal(5);
});Solution
Step 1: Parse the response JSON
The response has user.id = 5, sojsonData.user.idis 5.Step 2: Evaluate the assertion
The test assertsjsonData.user.idequals 5, which is true, so the test passes.Final Answer:
Test passes because user.id equals 5. -> Option AQuick Check:
Value matches assertion = Pass [OK]
- Misreading JSON structure
- Assuming test fails without checking value
- Confusing syntax errors with assertion failures
const data = pm.response.json();
pm.test("Check user name", () => {
pm.expect(data.user.name).to.equal('Bob')
});Solution
Step 1: Review syntax of Postman test code
The code usespm.testcorrectly, with proper arrow function and assertion syntax.Step 2: Check for syntax errors
Semicolons are optional in JavaScript; parentheses and function names are correct.Final Answer:
No error; the test code is correct. -> Option BQuick Check:
Correct syntax means no error [OK]
- Confusing pm.test with pm.tests
- Thinking semicolons are mandatory
- Missing parentheses in pm.expect
items contains an object with id equal to 10. Which test code correctly checks this in Postman?Solution
Step 1: Understand the response structure
itemsis an array of objects; we want to check if any object hasid10.Step 2: Evaluate each option
const items = pm.response.json().items; pm.expect(items.some(item => item.id === 10)).to.be.true; usessome()to check if any item hasid === 10, which is correct. pm.expect(pm.response.json().items.id).to.equal(10); wrongly accessesitems.id(invalid). pm.expect(pm.response.json().items.includes({id:10})).to.be.true; tries to useincludes()with an object, which won't work. const items = pm.response.json().items; pm.expect(items.find(id => id === 10)).to.exist; usesfind()but the callback is incorrect (should checkitem.id).Final Answer:
const items = pm.response.json().items; pm.expect(items.some(item => item.id === 10)).to.be.true; -> Option DQuick Check:
Use some() with correct callback for array check [OK]
- Using includes() with objects (doesn't work)
- Accessing array properties directly
- Incorrect callback function in find()
Ready to prove it?
Pass the test and this pattern moves unknown
