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 Send your first API request.
In the request, select the Scripts tab, then select Post-response.
In the Snippets section, select 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. In the response section, select Test Results 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: 2025/02/05
Additional resources
Videos
Blog posts
Public workspaces
Postman Academy