Assertions in Postman with Chai Assertion Library

PostmanREST APISOAP API

There are Assertions in Postman with Chai Assertion Library. Assertions are used to check whether the expected and actual results are the same. If not, an Assertion error is thrown with an error message.

An Assertion returns a Boolean value. In Postman, Assertion is implemented in JavaScript. It is available in the Postman application automatically.

In Postman, the Assertions are incorporated under the Tests or Pre-Request Script tab. The documentation of the Chai library is available in the link − https://www.chaijs.com/.

Let us build an Assertion to verify if the text – Postman is present in an array.

   pm.test["Text is present"], function(){
      pm.expect(['Java', 'Postman']).to.include('Postman')
   })

Output

Let us build an Assertion to check if an array contents any element.

   pm.test["Array contains element"], function(){
      pm.expect(['Java', 'Postman']).to.be.an('array').that.is.not .empty
   })

Output

Let us implement an Assertion to check object properties with eql function. This function performs comparisons between properties of two objects.

   pm.test("Equality", function(){
   let i = {
      "subject" : "Postman"
   };
   let j= {
      "subject" : "Cypress"
   };
   pm.expect(i).to.not.eql(j);

Output

The object i has the property - Postman while the object j property has the property - Cypress. So not.eql Assertion returned a true value.

raja
Published on 03-Aug-2021 14:20:43
Advertisements