pm.test('Status code is 200', () => {
pm.response.to.have.status(200);
});
const jsonData = pm.response.json();
pm.test('Response has user object', () => {
pm.expect(jsonData).to.have.property('user');
});
pm.test('User id is 123', () => {
pm.expect(jsonData.user).to.have.property('id', 123);
});
pm.test('User profile object exists', () => {
pm.expect(jsonData.user).to.have.property('profile');
});
pm.test('User profile name is John Doe', () => {
pm.expect(jsonData.user.profile).to.have.property('name', 'John Doe');
});
pm.test('User profile address object exists', () => {
pm.expect(jsonData.user.profile).to.have.property('address');
});
pm.test('User profile address city is New York', () => {
pm.expect(jsonData.user.profile.address).to.have.property('city', 'New York');
});
pm.test('User profile address zip is 10001', () => {
pm.expect(jsonData.user.profile.address).to.have.property('zip', '10001');
});This script uses Postman test scripts written in JavaScript to automate the manual test case.
First, it checks the HTTP status code is 200 using pm.response.to.have.status(200).
Then, it parses the JSON response body with pm.response.json() and stores it in jsonData.
Each pm.test block verifies a specific nested property. It first checks if the parent object exists before accessing child properties to avoid errors.
Assertions use pm.expect(...).to.have.property(...) to check both existence and exact values.
This approach ensures clear, step-by-step validation of nested objects, making debugging easier if a test fails.