Missed the train? : Assertions in API Testing using POSTMAN

Outstanding as one of the greatest REST client tools, POSTMAN takes place in an exponential involvement in testing as a part of continuous integration to track errors and nasty bugs while improving the predictability.

You will surely agree that POSTMAN is one of the most popular tools used in API testing, where the tests can be derived in your own custom way using JavaScript.

Of course API allows the communication and data exchange across different software systems or in between the layers of the application. But if you are still not handling your POSTMAN tests correctly, you might not get the fullest advantage of it.

POSTMAN test are totally relied on assertions based testing. Assertions are to be made on validating your end point’s expected response.

Your response could contain ,

  1. Any type of Data
  2. Status (Boolean)
  3. Request to another API function

Based on what you need to validate in your response body, you can have bunch of assertion test types in your script.

Let’s get started!

JavaScript tests in POSTMAN allow pm.test() function to write the specifications in postman test sandbox.

  1. Assert the Response Status Code

Every end point’s response contain its own response code. There we verify whether the status code is returned as expected.

you can assert multiple status codes at a time as follows.

2. Assert the Response Status

Along with the status code, it displays the status where we can always assert.

3. Assert the text in the JSON response

You can validate any string while checking whether that text contains in the JSON response.

4. Assert the response header

Header contains an additional information of HTTP requests which transfers to the server or user. When you go to the Headers tab in postman there you can pass values in different types of headers. E.g. Content-type, Date, Cookie, Server

5. Assert the Response Type

Response can be any type of data!

Mostly it will be a JSON body. If so, this is how you verify the Response Type.

6. Assert the response time

In a scenario which you want track the response time which is less than 100 milliseconds below is the way to write the test. Likewise you can arrange your test to verify the (to.be.above(value)) and (to.be.equal(value)).

7. Assert the environment

You can verify whether your current tests are set in the required environment or not.

8. Assert the cookie content

In the cookies we can verify whether it returns the cookie that we expect as below.

OR we can we can check the value of the cookie as below

9. Assert the array count in the response

Based on the response type, there can be situations where it returns the JSON response as an array. Using below snippet you can verify the array count.

10. Assert the value in the array response

Below is the example of verifying the value of the array response.

11. Verify whether the array is Empty

Also you can check whether array does not contain any values.

These are the mostly used assertions in API tests. You can develop your tests based on any type of above assertions.

Are you still thinking that you missed the train? Rather trying to catch the passed train, take the right one which will take you to the right path!

Your tickets are ready in the station!



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Rashmi Sandarekha

Software Quality Assurance Lead fascinated in Test Automation. Storyteller and a technical writer.