Enroll in Selenium Training

In the previous tutorial we have learnt about setting up tests against a API Endpoint in the Test and Collection chapter, there is a lot more to tests than just plain snippets or simple JavaScript. Assertions in programming language is a statement written such that it verifies whether the given predicate is true or false. A predicate is an expression which gives only boolean expression as the output viz. true or false. So in this tutorial taking assertions further we will learn about

  • Why to use Assertions?
  • Chai Assertion Library
  • How to write Assertions in Postman with Chai Assertion Library?

Why to use Assertions?

The sole purpose of a test is to identify that for a situation given parameters of the system are as expected. To force that the parameters of the system are correct we assert the expected values with the actual values during a test run. Assertions are used to assert that expected and actual values during a test run match. If they don't match, the test fails with the output pointing directly to the failure.

An assertion improves your test writing skills to a greater level. Postman provides JavaScript support to write tests which works under Postman Sandbox. As we learnt in the tutorial Set up Postman Tests, it is hard to write assertions or Functional methods in JavaS. In this tutorial we will learn how to write assertions using an external JavaScript library called Chai - Assertion Library. The assertions that we will be writing with this assertion library takes lot less effort compared to what we write directly in Javascript. The following image shows the difference with a very basic example.

Javascript Code

The above image contains a code where we are checking if a is equal to b or not. The same can be written with chai assertion library in the following manner.

Assertion_Expect_100_101

Isn't it so concise and easily readable? Think about the complex problems that we can easily write through this library.

An assertion is very useful in finding defects in the code as you can write an assertion just like a test, although they both are different. A test performs all the steps to reach to a particular state of the application and an assertion can validate the state of the application at that point. An assertion is very useful in finding defects in the application code. If you add assertions in the test, the test will fail once the assertions fails. But defining a more complex test in a mere easy way such as finding an element in an array will take just 2 lines of codes in assertion while it will take at least 5-10 lines in JavaScript tests. Reading a code also becomes very easy when we write assertions rather than writing the same thing in tests.

While writing assertions in Postman, there are two main steps involved:

  • Parse the response body: It is important to know what kind of response you are getting to perform a test on it. The most popular response is JSON, simply because it is very easy to read by humans and is machine readable also. It might happen that most of you might not even have to deal with any other response but that does not kill the fact that the response can be any format. There are many other formats of an HTTP response:
    • XML
    • HTML
    • Text
  • Write test code: Since we have already discussed about writing the tests in the test and collection runner tutorial we will not be covering it here. But, there we studied about the test writing in Javascript method or functional method. While we have to write only in Javascript because of the Postman Sandbox, there exist one library which makes it easier for us to write a test which would have taken more lines of code if written in Javascript. This library is Chai Assertion Library which we will talk about now.

But the assertion part is not confined to Chai Assertion Library. Chai Assertion is just a part of many assertions that Postman provide and also the only one being external to Postman. All the other Assertions works under Postman Sandbox which is of course Postman's. Taking the difficulty level in mind, chai assertion library is fairly easier than the other assertions and therefore in this tutorial we will be learning about the same. This tutorial will help you get familiar with the concept of Assertions so that in the next tutorial we can execute some difficult assertions.

Chai Assertion Library

Chai assertion Library is included by Postman by default in its application, so when you are writing chai assertions you don't have to worry about any other installation processes. The most amazing fact about assertions in Postman is that they write human readable tests. Tests written in assertions are so human readable that you might find it as a english sentence. All this makes your tests more easy to read and more friendly for humans. Although we are not needed to write very complex chai assertions as that are not required but we will cover the most common and frequently required assertions in Postman which will make your way complete while using this software.

Assertions in Postman with Chai Assertion Library

Although , if you want to learn more about Chai Assertion Library you can visit this link. In the next section we will learn about some assertions.

How to write Assertions in Postman using Chai Assertion Library?

If you have visited the above link, you would have found out that there are numerous assertions available in Chai library. We will be using some of them in the later section but in the this section of Assertion, we will make you understand the concept and assertions.

Assertion: Number is in array or not

1.Open the weather api in Postman

2.Write the following code in the tests tab

pm.test("Number included", function(){ pm.expect([1,2,3]).to.include(3); });

Assertion_Include_Number

Press enter and you will see the obvious response.

Assertion_Include_Number_Response

Yes we see the number is included because 1,2,3 has 3. It is obvious.

Assert: An array to be empty

1.Write the following code in the tests tab of weather api (or any api of your choice)

pm.test("Empty Array", function(){ pm.expect([2]).to.be.an('array').that.is.empty; });

Expect_Array_To_Be_Empty

Guess the response before pressing enter

Expect_Array_To_Be_Empty_Response

Okay. So you must have got pretty familiar now with the Chai Assertion Library. We will now show you one more assertion to conclude this tutorial.

Assertion: Verify objects

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

let a= {

"name" : "Harish"

};

let b= {

"name"  : "Harish"

}; * *pm.expect(a).to.eql(b);

});

Assertion_variable

Press send and see the results.

Assertion_variable_Response

It passes because the names are equal. But you might be wondering about equal and eql that we used above. Before clearing the air, let see the response for the same but with equal.

Write the same code as above and replacing eql with equal.

Assertion_variable_equal

Did you get the same response as in eql?

Assertion_variable_equal_Response

Although we are having the same code, eql and equal produces different responses. When we use equal we compare the objects created, which are different here i.e. a and b. While using eql we compare the properties of the objects, in this case name. As the two names are same the comparison passes. equal uses the === operator which is called Strict equality. While eql is Deep equality which compares the individual properties of the object.

Try editing the last line of the above code and write pm.expect(a).to.equal(a); and see what you get as a response.

So here we would like to conclude the topic of Chai Assertion Library. Practice more and more on the Chai Library Website. We will move on to our next tutorial now bout writing assertions executing under Postman Sandbox.

Pre-Request Script in Postman
Pre-Request Script in Postman
Previous Article
Different types of Asserts in Postman
Different types of Asserts in Postman
Next Article
Harish Rajora
I am a computer science engineer. I love to keep growing as the technological world grows. I feel there is no powerful tool than a computer to change the world in any way. Apart from my field of study, I like reading books a lot and developing new stuff.
Reviewers
Lakshay Sharma's Photo
Lakshay Sharma

Similar Articles

Feedback