- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to write Assertions in Postman with Chai Assertion Library?
We can write Assertions in Postman with Chai Assertion Library. Assertions are added in tests to verify if the actual and expected results are similar. In case, they are dissimilar, an Assertion error gets thrown along with the cause of the error.
Boolean value of either true or false is returned by an Assertion. In Postman, Assertion is handled by Chai Assertion Library which is developed in JavaScript. It is provided in the Postman application by default.
Assertions in Postman are added under the Tests tab. The documentation of the Chai library details are present in the link − https://www.chaijs.com/.
Let us create an Assertion to verify if a specific text – Postman is present in an array of strings.
pm.test["Text is present"], function(){ pm.expect(['Java', 'Postman']).to.include('Postman') })
Output −
Let us create another Assertion to verify if an array contains elements.
pm.test["Array contains element"], function(){ pm.expect(['Java', 'Postman']).to.be.an('array').that.is.not .empty })
Output −
Let us implement an Assertion for verification of objects with eql function. This function compares properties of one object with another.
pm.test("Equality", function(){ let i = { "subject" : "Postman" }; let j= { "subject" : "Cypress" }; pm.expect(i).to.not.eql(j);
Output −
The property declared for object i is Postman while the property declared for j is Cypress. Hence not.eql Assertion gives a true result.
- Related Articles
- Assertions in Postman with Chai Assertion Library
- What are assertions in Selenium with python?
- How to write an image using Java OpenCV library?
- How to set a Test in Postman with JavaScript Method?
- How to handle Assertion Error in Java?
- Assertions in Java
- Assertions in Python
- Assertions in C#
- How to catch an assertion error in Java
- How to Import Collection in Postman?
- How to Monitor Collections in Postman?
- How to use Sessions in Postman?
- How to Manage Cookies in Postman?
- How to Add Cookies in Postman?
- How to create sessions in Postman?
