Write your first test

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 test scripts 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:

  1. Go to the request you made in sending your first request.

  2. In the request, go to the Tests tab.

  3. 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);
    });
    
  4. 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 Writing tests.

Last modified: 2023/07/26