Postman Tutorial Part 38 – Mostly Used Assertion Code Snippets in Postman to Achieve API Automation

When we hit any request in Postman, we should verify response code, response time , value of an attribute in response etc. These assertion helps to test the expected behavior of API. In this post, we will see some mostly used code snippets in Postman scripts to validate response.

Code snippet to assert status code of API:-

// To verify if the status code is exactly same to 200
pm.test("Status code is 200", function () {
    // Using Response Assertion API available in the test scripts
    pm.response.to.have.status(200);
    // Using explicit expect
    pm.expect(pm.response.code).to.eql(200);
});

Code snippet to assert name of status code of API:-

// To verify if the status code contains a string. For ex:- 201 (created), 200(OK)
pm.test("Status code contains name", function () {
    // Using Response Assertion API available in the test scripts
    pm.response.to.have.status("OK");
    // Using explicit expect
    pm.expect(pm.response.status).to.eql("OK");
});

Code snippet to assert if status code is from a list of expected status code:

// To verify if the status code is from a list of expected status code
pm.test("Status code is either 200 or 201", function () {
    pm.expect(pm.response.code).to.be.oneOf([201,200]);
});

Code snippet to assert by ignoring case:-

//To ignore case sensitive of string
pm.test("Status code contains name", function () {
    pm.expect(pm.response.status.toLowerCase()).to.eql("OK".toLowerCase());
});

Code snippet to assert response time is below a value:-

// To verify response time is less than a value
pm.test("Response time is less than 1200ms", function () {
    pm.expect(pm.response.responseTime).to.be.below(1200);
});

Code snippet to assert response time is above a value:-

// To verify response time is above than a value
pm.test("Response time is above than 200ms", function () {
    pm.expect(pm.response.responseTime).to.be.above(20);
});

Code snippet to assert response time is between a range:-

// To verify response time is in between
pm.test("Response time is in between 20 and 1200ms", function () {
    pm.expect(_.inRange(pm.response.responseTime, 20, 1201)).to.eql(true);
});

Code snippet to assert response size is not zero:-

// To verify response size is not zero
pm.test("Response size is not zero", function () {
    pm.expect(pm.response.responseSize).to.not.equal(0);
});

Code snippet to assert presence of a string anywhere in response:-

// To assert response contains a string
pm.test("Body matches string", function () {
    pm.expect(pm.response.text()).to.include("michael.lawson@reqres.in");
});

Code snippet to assert presence of a header in response:-

// To check if any header is present in response
pm.test("Content-Type is present", function () {
    pm.response.to.have.header("Content-Type");
});

Code snippet to assert value of a response header:-

// To assert value of a header
pm.test("Value of Content-Type", function () {
    pm.response.to.be.header("Content-Type","application/json; charset=utf-8");
    pm.expect(pm.response.headers.get("Content-Type")).to.eql("application/json; charset=utf-8");
});

Code snippet to assert presence of a cookie:-

pm.test("Presence of cookies",function(){
    pm.expect(pm.cookies.has('__cfduid')).to.be.true;
    pm.expect(pm.cookies.has('someNonExistCookie')).to.be.false;
})

Code snippet to assert value of a cookie:-

pm.test("Assert value of cookies",function(){
    pm.expect(pm.cookies.get('__cfduid')).to.eql("dc3773fcc3c41c26665ec1903a18adcb91542173209");
})

Code snippet to assert response body is a JSON Object or JSON array:-

// To assert if response body is an object or array
pm.test("Response body is a json object or array",function(){
    pm.expect(pm.response.json()).to.be.an('object')
     pm.expect(pm.response.json()).to.be.an('array')
})

Code snippet to assert exact response body:-

// To assert exact respone body
pm.test("Assert exact response body",function(){
    pm.response.to.have.body('{'+
            '"id": 8,'+
            '"email": "lindsay.ferguson@reqres.in"');
})

Note:- JavaScript does not support multiline string. So you must need to use concatenation as shown above.

#ThanksForReading

Have you tested your Selenium knowledge ? Try below Selenium Quiz:-

Learn Selenium with Quiz

Be master in Selenium-WebDriver

API Testing – Basics

API Testing – Postman

API Testing – Rest Assured

Learn Protractor Step by Step

Frequently Asked Java Programs in Interview

1 thought on “Postman Tutorial Part 38 – Mostly Used Assertion Code Snippets in Postman to Achieve API Automation”

Leave a Reply

Your email address will not be published. Required fields are marked *