Writing your first test in Postman

Mitul Vaghela
5 min readJun 21, 2020

If you have ever undertaken API testing, chances are that you might have heard about Postman. Many developers and testers I know use Postman to make GET/PUT/POST requests and then they manually validate the response. While Postman’s GUI is really great and intuitive, very few people know that you can actually do much more in Postman than just firing requests and exploring the response.

Ability to write automated tests is one of those lesser known features of Postman. In this article, I am going to introduce you to the Postman GUI and help you write your first test.

For this demonstration, we need an API and we will use the pet store API in this article. Here are a few endpoints we will use in this article —

GET — https://petstore.swagger.io/v2/pet/{petId} — get details about a pet by pet id

POST —https://petstore.swagger.io/v2 /pet/{petId} — update pet details by pet id

First things first, lets download postman from here — https://www.postman.com/downloads/

Download Postman

After download is successful, open the Postman app and click “Skip signing in and take me straight to the app” as highlighted in the screenshot below.

skip signing in

On the next screen, click New → Request.

create new request

You should see a new request pop up as shown in screenshot below. Enter the request name and optional description

Provide request details

Now, before we can save this request, we need to create a new “Collection”. A collection in Postman is basically a folder to store your requests. It provides you with an organizing mechanism that can be used to club requests that have a common goal. For example, you can club all requests belonging to a certain user story under one collection or you can club requests that are part of a workflow and dependant on one another under a collection.

Click on Create Collection. Provide a meaningful name and click on tick mark to save it. In this case, we will name the collection as MODIFY_PET_DETAILS as ultimately we want to validate the update pet feature. Note that in this article, we will not implement the entire “update pet feature test”. We will limit it to a simple test. We will continue writing the “update pet feature” in a follow up article.

create collection

After that, click on create button and your request should be successfully created.

Save the request

The newly created request looks like this —

new request screen

In the request url, let’s add our GET PET request and click on send button. We should get the response.

get pet details in response

Perfect! so we have now reached a point where we can start writing tests. Click on the “Tests” tab located in the panel immediately below the request url.

location of Tests tab

Your “Tests” tab looks like this —

It’s important to note that all Postman tests are written in JavaScript only.

For our first test, let’s quickly add an existing script provided by Postman. We will validate the HTTP status code of our response.

Your first test

Let’s look at the anatomy of the test. The test calls a test function on pm object. The test function takes two arguments -

  1. Name of the test
  2. Function where the actual test code is written

pm object is the global object that provides Postman’s test execution context. It gives you details on requests, response, environment variables and many more things.

It’s time to run your first test. The execution of tests starts only after the response is received. So you should not use the test code to manipulate the outgoing request. If you need to execute code before a request runs, use Pre-request Scripts instead.

You don’t need to explicitly start Postman tests unlike other tools. Just click on the send button and tests will automatically start once response is received. So let’s click on send button and observe the response section.

First Test Result

Lets click on the Test Results tab.

Exploring test results pane

Now, let’s add one more test and make it fail. This way we can see both passing and failing tests and hence get a clearer picture of Postman’s reporting capabilities. This time let’s add a test that validates the response time. Again, scroll down in the list of existing tests till you see this test. The name of the test should be “Response time is less than 200ms”. Click on it once you see it. This will add the test to the editor. Once added, it should look like this. Change the number as explained in the screenshot.

Adding one more test

Once the number is updated, click on send button again.

First failure
Failed test details

Thats all for today. Thank you for reading this article. In the follow up article, we will continue to write our “update pet feature test”.

I hope this article helped you to write your first test. If you are facing any difficulties, please let me know in the comment section and I will try my best to resolve them.

--

--