Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

postman

How to use tests in Postman

Anjana Shankar

Overview

To learn how postman can be installed and used to test our REST APIs, follow this answer. We can take this testing a step further by adding tests executed when we get a response from the API.

Along with the Collection Runner, these tests give us a strong foundation for API testing and improve the development cycle considerably.

We can go to the test tab for any API and write our tests. We can use the Javascript syntax for the same.

Postman Test tab
Postman Test tab

The pm library in Postman helps write the tests. A test for a response code of 200 would look like the code below:

pm.test("Status code is 200", function () {
  pm.response.to.have.status(200);
});
  • Line 1: We use pm.test. The first parameter is the name of the test. The second parameter is the test function.
  • Line 2: The pm.response gets populated by the response of the API call. We can add more tests to validate the response.

The test is executed every single time the API request is made.

In addition to test scripts, we also have pre-request script These can be used for any actions that need to be done prior to request execution.

Order of script execution

Conclusion

Postman tests are a powerful addition to the development cycle and when used correctly, they reduce the time for feature development considerably by providing consistent testing.

RELATED TAGS

postman
RELATED COURSES

View all Courses

Keep Exploring