API tests are a way to ensure that your API is behaving as you expect it to. For example, you might write a test to validate your API's error handling by sending a request with incomplete data or wrong parameters. You can write tests for your Postman API requests in JavaScript and add them to individual requests, collections, and folders in a collection. Postman includes code snippets you can add and then change to suit your test logic.
To write a test, do the following:
Go to the request you made in sending your first request.
In the request, go to the Scripts tab, then select the Post-response tab.
In the snippet section to the right, select the snippet Status code: Code is 200. This will enter the following test code:
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
Select Send.
After the request runs, the test will run. Go to the Test Results section of the response to review the results of your test.
To learn more about writing tests, go to Write scripts to test API response data in Postman.
Last modified: 2023/07/26
Additional resources
Videos
Blog posts
Public workspaces
Postman Academy