0
0
Postmantesting~15 mins

Why automated assertions validate responses in Postman - Automation Benefits in Action

Choose your learning style9 modes available
Validate API response status and body using assertions
Preconditions (2)
Step 1: Send a GET request to https://api.example.com/users
Step 2: Check that the response status code is 200
Step 3: Check that the response body contains a JSON array
Step 4: Check that the first user object has a 'name' property
✅ Expected Result: The response status code is 200, the body is a JSON array, and the first user object contains a 'name' property
Automation Requirements - Postman Tests
Assertions Needed:
Verify response status code equals 200
Verify response body is an array
Verify first element in response body has 'name' property
Best Practices:
Use pm.response.to.have.status for status code assertion
Use pm.expect with JSON parsing for body content assertions
Write clear and simple assertion messages
Avoid hardcoding values that may change frequently
Automated Solution
Postman
pm.test('Status code is 200', () => {
    pm.response.to.have.status(200);
});

pm.test('Response body is an array', () => {
    const jsonData = pm.response.json();
    pm.expect(jsonData).to.be.an('array');
});

pm.test('First user has a name property', () => {
    const jsonData = pm.response.json();
    pm.expect(jsonData[0]).to.have.property('name');
});

The first test checks the HTTP status code to confirm the request succeeded.

The second test parses the response body as JSON and asserts it is an array, matching the expected data structure.

The third test verifies that the first user object in the array contains a 'name' property, ensuring the data includes expected fields.

Using pm.test and pm.expect provides clear, readable assertions that automatically validate the API response during test runs.

Common Mistakes - 3 Pitfalls
Not parsing the response body as JSON before assertions
Hardcoding status codes or response values without flexibility
Skipping status code verification
Bonus Challenge

Now add data-driven testing with 3 different API endpoints and validate their responses similarly

Show Hint