Or press ESC to close.

Test scripts in Postman

Dec 25th 2022 4 min read
easy
api
javascriptES6
web
postman10.6.0

Postman is easily one of the most popular software testing tools out there. I'm being modest when I say it can build, design, test, and document APIs, since it has a lot more features that help us when working with them.

One of those features is automation testing. That's right. Postman can even execute automated tests, which can save us time when verifying that our APIs still work as intended. Its runtime allows us to write API tests in JavaScript before a request is sent to the server (pre-request script) and after a response has been received (test script).

In this post we will focus on test scripts, but the same applies for pre-request scripts too. The only difference is the time of execution.

To get started, we will open Postman and create a new HTTP request. For demonstration purposes, we will use a simple GET request from the Petstore Swagger API:

a GET request and response in Postman

After hitting the Send button, we get a 200 OK response and some data in the response body. Let's see how we can verify that we got the expected response code and that the data from the response matches our expectations.

Right beneath the request type and URL, we can see a couple of tabs. By clicking on the 'Tests' tab, we will be navigated to the Test editor, where we write our scripts:

the Tests tab in Postman

The easiest way to start writing a test is by using the pm library. At the same time, it can execute the test method and access Postman data like requests, responses, cookies, environments, and more.

In our example, we will test that our response code is indeed 200 and that the 'available' property from our response has a value higher than 350. To do that, we will first call the test method from the pm library. The method takes two parameters. The first one is the name of our test and the second one is a function containing the required assertions:

              
pm.test("Verify response code and property value", function () {
  //required assertions
});
            

The assertions can use BDD chains with the help of the Chai Assertion Library, which provides better readability.

To test our first case, we will again use the pm library to access the response object. This object contains all the response data from our GET request. And after using BDD chains, the assertion will look like this:

              
pm.response.to.have.status(200);
            

For the second case, we will again use the response object to access the wanted property and assert its value. This time we will need to parse the JSON response to access its properties:

              
const responseData = pm.response.json();
pm.expect(responseData.available).to.be.above(350);
            

After combining both of the assertions, the test method looks like this:

              
pm.test("Verify response code and property value", function () {
  const response = pm.response;
  response.to.have.status(200);
  pm.expect(response.json().available).to.be.above(350);
});
            

Now we just need to hit the Send button again, and our test results will be displayed in the 'Test Results' tab:

test results in Postman

We can see that our test passed. To check how the results look in case of a failure, we will change the expected status code from 200 to 404:

test failure in Postman

We can see that our test failed because one of our assertions failed and we get a nicely formated error message.

This was just a small example that showed how to verify a response code and parsed JSON data. In the same way, we can test cookies, response times, headers, and more. But what makes this feature powerful is the ability to combine it with other Postman features like Collections and Environments.

We can create a collection with multiple requests. Each request will have its tests that will verify different response properties:

requests in a Postman collection

In the collection itself, we will add a test to verify the response code of the request:

test script in a collection

Now if we click the three dots next to the collection name and choose the 'Run collection' option, we will get an output like this one:

executing all collection tests

We can see that the runner executed the collection test (response code verification) for each request in the collection, plus the individual tests. We wrote three individual tests and one for the collection, which resulted in six tests being executed.

Test scripts in Postman allow for an easy way to execute API tests on individual HTTP requests or a collection of them.



A test script will run after a request, while a pre-request script will run before it.

That's it. All tests passed. If you want to learn more about writing tests in Postman, visit the official documentation. 🙂