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.

pm.test("Verify the status code is 200", function () {

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

pm.test(“Successful POST request”, function () {

2. Assert the Response Status

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

pm.test(“Status is OK”, function () {

3. Assert the text in the JSON response

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

pm.test(“Body matches string”, function () {

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

pm.test(“Content-Type is present”, function () {

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.

pm.test(“Response if Json”, function(){

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)).

pm.test(“Response time is less than 100ms”, function () {

7. Assert the environment

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

pm.test("environment to be production", function () {pm.expect(pm.environment.get("env")).to.equal("production");});

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

pm.test(“Cookies_Value_Check”, function(){

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.

pm.test(“ISBN Count”, function () {

10. Assert the value in the array response

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

pm.test(“Test Name”, function () {
var result;
for (var loop = 0; loop < pm.response.json().arrayName.length; loop++)
if (pm.response.json().arrayName[loop].arrayElement=== pm.variables.get(“arrayElementValue”)){

11. Verify whether the array is Empty

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

pm.test(“Empty Array”, function(){

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!

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