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.
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);});
pm.test
. The first parameter is the name of the test. The second parameter is the test function.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.
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.