How to Assert Response Time in Postman Tests
In Postman, you can assert response time by writing a test script using
pm.response.responseTime. Use pm.test with an assertion like pm.expect(pm.response.responseTime).to.be.below(200) to check if the response time is under 200 milliseconds.Syntax
Use the following syntax inside the Tests tab in Postman to assert response time:
pm.response.responseTime: Gets the response time in milliseconds.pm.test(name, function): Defines a test with a descriptive name.pm.expect(value).to.be.below(number): Asserts that the value is less than the given number.
javascript
pm.test('Response time is less than 200ms', () => { pm.expect(pm.response.responseTime).to.be.below(200); });
Example
This example shows how to assert that the API response time is below 300 milliseconds. If the response time is greater, the test will fail and show an error in the Postman test results.
javascript
pm.test('Response time is less than 300ms', () => { pm.expect(pm.response.responseTime).to.be.below(300); });
Output
PASS - Response time is less than 300ms
Common Pitfalls
Common mistakes when asserting response time in Postman include:
- Using incorrect property names like
pm.response.timeinstead ofpm.response.responseTime. - Not wrapping the assertion inside
pm.test, which means the test won't be recorded. - Setting unrealistic response time limits that cause frequent test failures.
javascript
/* Wrong way: Missing pm.test wrapper */ pm.expect(pm.response.responseTime).to.be.below(200); /* Right way: Wrap assertion inside pm.test */ pm.test('Response time is below 200ms', () => { pm.expect(pm.response.responseTime).to.be.below(200); });
Quick Reference
| Concept | Description | Example |
|---|---|---|
| pm.response.responseTime | Gets response time in milliseconds | pm.response.responseTime |
| pm.test | Defines a test case | pm.test('Test name', () => { ... }) |
| pm.expect(...).to.be.below(number) | Asserts value is less than number | pm.expect(pm.response.responseTime).to.be.below(200) |
Key Takeaways
Always use pm.response.responseTime to get the API response time in Postman.
Wrap your assertions inside pm.test to record test results properly.
Set realistic response time limits to avoid false test failures.
Use pm.expect(...).to.be.below(value) to assert response time is under a threshold.
Check Postman test results tab to see pass or fail status of your response time tests.