top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Assertions: Validations in POSTMAN


Assertion plays a very important role to make Postman a great utility tool to create integration test for rest API end points.

The output of assertion will decide if a test can either pass or fail.


For response validation there are few important things to know. Before sending request we have to know what type of response and components of response we are getting. Without knowing the response we cannot add the validation as you don’t know what we have to validate.

So before testing the APIs we should have a clarity on what kind of request we are sending ,what type of data we are sending and what kind of data we are getting as a response.

For this , before adding the validations you need to run the API at least once. You have to send the request to check for response body, header, cookies etc. are correct or not.

So, to put validations we have to consider these few important things:

To test these items we need to add assertion. Assertions are nothing but validation points. To add these assertion in Postman there is one library provided by Postman i.e. pm.

pm is a postman extraordinaire library in which many functions are available. These functions are written in JavaScript.



How to write Assertion in Postman

We need to write our own JavaScript function in which we call the postman(pm) function. Postman uses Chai assertion library for creating assertion. Chai is a BDD/TDD assertion library which makes it highly readable and understandable.


There are two ways to write the function.


Normal Function




pm.test("Test Name", function() here

{ pm : postman

//write assertion here; test :predefined function

} Test Name: Any user defined name

); function() : is the keyword



Arrow Function





pm.test("Test Name", () => Instead of function() keyword we can use ()=>

{

// assertion;

}

);

Arrow functions are more popular one. Using these functions we can write validation point for each components



Where to write these functions ?


In the Postman tool


  • Open the request.

  • Select Scripts.

  • Select Post-Response

    Here we write all validation points.


Let's explore the various types of assertions in Postman.

1. Status Code Assertion

  • Purpose: Verify that the response returns the expected HTTP status code.

  • Example:


If status code is 200 in response body, this assertion will pass otherwise it fails .To see the test result, you can click on the Test Result tab after you send the request


Let's change the status code in the assertion to 201. As we are expecting 200 it will fail the assertion.


This allows us to verify validations. Similarly, we can set validation points for other components of the response body and check for pass or fail assertions.



If you want to test for the status code being one of a set, include them all in an array and use one of pm.test("Successful POST request", () =>

{

pm.expect(pm.response.code).to.be.oneOf([200,201]);

}

);

Here if status code contains any of the status either 200 or 201 , the assertion will pass. You can give any number of possible status code for that particular request.



We can also check the status code text. The response body contains status code along with message .

pm.test("Status code name has string", () =>

{

pm.response.to.have.status("Created");

}

);


Now, let's move on to other assertions.

2. Response Time Assertion


  • Purpose: Ensure that the API responds within a specified time limit.

  • Example:

If the response time is more than 200ms , the assertion will fail.

3. Response Body Assertion


Response body assertion can be done in many ways like:

  • Checking for data type

  • Checking for values of the response body

    Let's have a look how to put validations for the same

  • Purpose: Validate the datatype of the response body.

  • Example: Check if the response body contains a specific datatype.


var jsonData = pm.response.json();

This line of code will copy the response body from pm to jsonData variable. This variable than be parsed to check all validation points on response body.


The response body may include multiple fields like "id", "name", etc. We can validate the types of values that these fields can accept.


We can also check for array

 pm.expect(jsonData.courses).to.be.an("array")


Asserting array properties

Check if an array is empty and if it contains particular items:

{

"id": 1,

"name": "John",

"location": "India",

"phone": "1234567890",

"courses": [ "Java", "Selenium" ]

}

Consider we have above response body and we need to check for array contents:


In this way we can check for a particular array all listed values.


4. Header Assertion

In the request Header tab we can check for content type

  • Purpose: Ensure that the response contains specific headers with the expected values.

  • Example:


5. Cookie Assertion

  • Purpose: Validate the presence and value of cookies in the response.

  • Example:


Test if a cookie is present in the response:
 Test for a particular cookie value:


6. Chaining Multiple Assertions

  • Purpose: Combine multiple assertions in a single test.

  • Example:



7. Schema Validation

  • Purpose: Validate the JSON response body against a predefined schema.

  • Example:


These assertions help ensure your API behaves as expected across various scenarios. They are essential for automated testing in Postman, allowing you to catch errors early in the development process.

BONUS TIP


To optimize your code, you can write assertions that are commonly repeated across multiple requests at the collection level and call them as functions whenever needed.

At the collection level under the Script tag you can write your reusable code

Ending Note

In conclusion, using assertions in Postman is key to effective API testing. They help you ensure that your APIs work correctly, catch issues early, and keep your application quality high. Whether you're checking response codes, headers, or JSON data, assertions make your testing process more reliable and efficient. By incorporating these techniques, you can create more streamlined and effective API tests.


7 views0 comments

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page