0
0
Postmantesting~15 mins

Response time assertions in Postman - Build an Automation Script

Choose your learning style9 modes available
Verify API response time is under 500 milliseconds
Preconditions (2)
Step 1: Open Postman and create a new GET request
Step 2: Enter the API endpoint URL in the request URL field
Step 3: Send the request by clicking the Send button
Step 4: Observe the response time shown in Postman
Step 5: Verify that the response time is less than 500 milliseconds
✅ Expected Result: The API responds successfully and the response time is less than 500 milliseconds
Automation Requirements - Postman Tests (JavaScript)
Assertions Needed:
Response status code is 200
Response time is less than 500 milliseconds
Best Practices:
Use pm.response.to.have.status for status code assertion
Use pm.expect(pm.response.responseTime).to.be.below(500) for response time assertion
Write clear and simple test scripts inside the Tests tab
Avoid hardcoding values except for thresholds like 500 ms
Automated Solution
Postman
pm.test('Status code is 200', () => {
    pm.response.to.have.status(200);
});

pm.test('Response time is less than 500 ms', () => {
    pm.expect(pm.response.responseTime).to.be.below(500);
});

The first test checks that the API returns a status code 200, which means success.

The second test checks that the response time is below 500 milliseconds using pm.response.responseTime.

Both tests use Postman's built-in pm object and chai assertion library for clear and readable assertions.

This script should be placed in the Tests tab of the Postman request.

Common Mistakes - 3 Pitfalls
Checking response time with incorrect property like pm.response.time
Using hardcoded status codes without assertion methods
{'mistake': 'Not placing the test script inside the Tests tab', 'why_bad': "Scripts outside the Tests tab won't run after the request, so no assertions happen.", 'correct_approach': 'Always write test scripts inside the Tests tab of the Postman request.'}
Bonus Challenge

Now add data-driven testing with 3 different API endpoints and verify each response time is under 500 ms

Show Hint