0
0
PostmanHow-ToBeginner ยท 3 min read

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.time instead of pm.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

ConceptDescriptionExample
pm.response.responseTimeGets response time in millisecondspm.response.responseTime
pm.testDefines a test casepm.test('Test name', () => { ... })
pm.expect(...).to.be.below(number)Asserts value is less than numberpm.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.