Chai Assertion Library
Chai assertion library is an external javascript library used to write assertions. Compared to what we write directly in javascript, this assertion library needs less time & effort and easy use.
Chai assertion library is available by default in the Postman. So when you are writing chai assertions, then don’t worry about other processes of installation. The main feature of Postman assertions is that they write the tests with English sentences, which is human readable. So it is very easy to read and user friendly.
If you would like to learn more about the Chai Assertion Library, there is an official website for that. You can visit this https://www.chaijs.com/.
Writing Assertions using Chai Assertion Library.
Copy the following URL in the URL text field.
Example 1:
- Go to the Tests tab and write the following code in the Tests editor.
- Click on Send and see the Test Results from the response section.
In the above example, we are checking that if 100 is equal to 200 or not. The response is failed, which is obvious.
Just edit the code with the following code and see the response again.
Since 100 equal to 100. The test is passed.
Example 2:
Here, we will check the given number is in an array or not.
- Open the tests tab and write the following code:
- Press send and see the Test Results tab from the response.
Here, you can see that the test is passed because, in the array list of 100, 200, and 300 has 300.
Example 3:
In this assertion, we will verify the object.
- Open the tests tab and write the following code:
- Click on the Send button and see the Test Results from the response.
The test is passed because on both objects, names are equal.
In the above code, we have used eql(). Postman offers both methods eql() and equal(). But both methods are different. equal() is used to compare the objects, and eql() is used to compare the properties of the objects. In this case, eql() compared the name property of the object.
Try to edit the last line of code with pm.expect(x).to.equal(y), and see the response.
The above test failed because we have used equal(), which compares the object, not the property of the object.