Enroll in Selenium Training

In the previous tutorial of Chai Assertion Library  we covered Postman Assertions using expect keyword. Also there are many other assertions in Postman which works around Postman Sandbox which we covered in pre-request scripts in Postman. In this tutorial we will talk about some common Different types of Asserts in Postman.

In this tutorial we will be focusing on the response part because it is extremely important as we almost all the time need to assert on the response. So we will apply assertions on:

  • Response time
  • Status Code
  • Status code meaning
  • Response Type
  • Response header
  • Post method check
  • String in Response

Different types of Asserts in Postman

For instance we can think of sending a request and checking all the above stated things on the same. In the end of this tutorial, you can also add all the assertions in one single request to practice and improve your skills. So, we will start now.

Prerequisite:

Assert on Response Time

This assert helps us to verify the Response Time of the Request. Below we are verifying that if the Response Time is less than 100ms. Go to the Tests tab and write the following code:

pm.test("Response time is less than 100ms", function () {      pm.expect(pm.response.responseTime).to.be.below(100); });

Response_Code_Chai

NOTE: This assertion can also be modified to check the time to be above a certain value (to.be.above(value)) and equal to a certain value (to.be.equal(value)).

Press Send and see the response.

Response_Code_Chai_Response

Note: In the above case, Assert got failed, as the response time was 1121ms. Also, the same is visible clearly in the response box as AssertionError: expected 1121 to be below 100 which is false obviously.

Assert on Response Status Code

This assertion is based on checking the Response Status Code. In the below test,  we are verifying that if the Response Status Code is 200. Test will PASS in case of Status Code 200, else it will FAIL in case of any Status Code other than 200. Write the following code in the Tests tab:

pm.test("Status code is 200", function () {      pm.response.to.have.status(200); });

Status_Code_200_Chai

You can place any status code inside the value box to check the value of the status. The same can also be expressed in Chai Assertion Library as

For checking status being OK

pm.test("Status is OK", function () {      pm.response.to.be.ok; });

For checking status being BAD REQUEST

pm.test("Status is Bad Request", function () {      pm.response.to.be.badRequest; });

Press send and see the response which is true in my case.

Status_Code_200_Chai_Response

We got the response status code to be 200 and hence our assertion has passed.

Assert on Response Status Code Meaning

This assertion is based on checking a specific property. In this assertion we will check a specific property and its value. In the example given below we are checking the property status and its value being OK.

Write the following code inside Tests tab.

pm.test("Status is OK", function(){      pm.response.to.have.property('status', 'OK'); });

Status_Code_OK_Chai

Press Send and see the result which will be true in my case.

Status_Code_OK_Chai_Response

This one was quite understandable, I guess.

Assert on Response Type

This assertion is based on verifying the Response Type. In the below test,  we are verifying that if the Response Type is JSON. Write the following code in the Tests tab:

pm.test("Response if Json", function(){      pm.response.to.be.json; });

Response_Json_Chai

Note: I hope you remember that in Get Request when we sent the request using weather api, we received the response in the text format rather than JSON format. We are using the same API here.

Press Send and see the result.

Response_Json_Chai_Response

The assertion has failed because of the response type. We expected response type to be JSON, but the response that we get in weather api is in the TEXT format.

Assert on Response Header

This assertion is based on checking whether the header has content-type or not.

Write the following in your tests tab

pm.test("Content-Type is present", function () {      pm.response.to.have.header("Content-Type"); });

Response_To_Have_Content_Type

This assertion checks if the content-type header is present in the response or not. Press Send and see if it is or not.

Response_To_Have_Content_Type_Response

Yes, the test passed. But, how can we check if it was really present or not. As you can see besides Test Results, Headers is written. Go to Headers and Content-Type must be present there.

Content_Type_Header

So now we have seen the assertions that are commonly used. Now, we will try to use both Chai Assertion along with these assertions to create some meaningful tests.

Assert for Multiple Status Code

For this we will be using the customer register api since it uses POST method type to send the request or you can also use Weather API but ultimately the test will fail. You can download both the APIs from here.

Go to tests tab and write the following code

pm.test("Successful POST request", function () {      pm.expect(pm.response.code).to.be.oneOf([201,202]); });

Successful_Post_Request

Note: 201 is created and 202 is Accepted.

Press send and see the response which will be pass if the status code is 201 or 202 or else will fail.

Assert on Response Text

Check if response contains a string

Write the following code in the tests tab of any API which is correct and gives response.

pm.test("Body matches string", function () {      pm.expect(pm.response.text()).to.include("string_you_want_to_search"); });

Replace the query "string_you_want_to_search" with the string you want to search. If your response will contain the string your assertion will pass or else fail.

Press send and see the response.

For learning more about it we strongly recommend reading the documentation of Chai Library and Postman Sandbox.

So in this tutorial we asserted some assertions which executes under Postman Sandbox. These assertions are very helpful for a tester as he can complete his testing more easily and effectively. I hope you have got an idea of what assertions are and how to use them. You can combine the both Chai assertion and postman's assertion to create your own custom assertions and see if you got it right or not. We will move onto our next tutorial now. Till then, keep practicing assertions. They are the heart of testing through Postman.

Assertions in Postman with Chai Assertion Library
Assertions in Postman with Chai Assertion Library
Previous Article
Mock Server in Postman
Mock Server in Postman
Next Article

Similar Articles