Missed the train? : Assertions in API Testing using POSTMAN

Outstanding as one of the greatest REST client tools, POSTMAN takes place in an exponential involvement in testing as a part of continuous integration to track errors and nasty bugs while improving the predictability.

You will surely agree that POSTMAN is one of the most popular tools used in API testing, where the tests can be derived in your own custom way using JavaScript.

Of course API allows the communication and data exchange across different software systems or in between the layers of the application. But if you are still not handling your POSTMAN tests correctly, you might not get the fullest advantage of it.

POSTMAN test are totally relied on assertions based testing. Assertions are to be made on validating your end point’s expected response.

Your response could contain ,

  1. Any type of Data
  2. Status (Boolean)
  3. Request to another API function

Based on what you need to validate in your response body, you can have bunch of assertion test types in your script.

Let’s get started!

JavaScript tests in POSTMAN allow pm.test() function to write the specifications in postman test sandbox.

  1. Assert the Response Status Code

Every end point’s response contain its own response code. There we verify whether the status code is returned as expected.

pm.test("Verify the status code is 200", function () {
pm.response.to.have.status(200);
});

you can assert multiple status codes at a time as follows.

pm.test(“Successful POST request”, function () {
pm.expect(pm.response.code).to.be.oneOf([201,202]);
});

2. Assert the Response Status

Along with the status code, it displays the status where we can always assert.

pm.test(“Status is OK”, function () {
pm.response.to.be.ok;
});

3. Assert the text in the JSON response

You can validate any string while checking whether that text contains in the JSON response.

pm.test(“Body matches string”, function () {
pm.expect(pm.response.text()).to.include(“string_you_want_to_search”);
});

4. Assert the response header

Header contains an additional information of HTTP requests which transfers to the server or user. When you go to the Headers tab in postman there you can pass values in different types of headers. E.g. Content-type, Date, Cookie, Server

pm.test(“Content-Type is present”, function () {
pm.response.to.have.header(“Content-Type”);
});

5. Assert the Response Type

Response can be any type of data!

Mostly it will be a JSON body. If so, this is how you verify the Response Type.

pm.test(“Response if Json”, function(){
pm.response.to.be.json;
});

6. Assert the response time

In a scenario which you want track the response time which is less than 100 milliseconds below is the way to write the test. Likewise you can arrange your test to verify the (to.be.above(value)) and (to.be.equal(value)).

pm.test(“Response time is less than 100ms”, function () {
pm.expect(pm.response.responseTime).to.be.below(100);
});

7. Assert the environment

You can verify whether your current tests are set in the required environment or not.

pm.test("environment to be production", function () {pm.expect(pm.environment.get("env")).to.equal("production");});

8. Assert the cookie content

In the cookies we can verify whether it returns the cookie that we expect as below.

pm.expect(pm.cookies.has(‘sessionID’)).to.be.true;

OR we can we can check the value of the cookie as below

pm.test(“Cookies_Value_Check”, function(){
pm.expect(pm.cookies.get(‘sessionID’)).to.eql(‘abc123’);
});

9. Assert the array count in the response

Based on the response type, there can be situations where it returns the JSON response as an array. Using below snippet you can verify the array count.

pm.test(“ISBN Count”, function () {
pm.expect(2).to.eql(pm.response.json().arrayName.length);
});

10. Assert the value in the array response

Below is the example of verifying the value of the array response.

pm.test(“Test Name”, function () {
var result;
for (var loop = 0; loop < pm.response.json().arrayName.length; loop++)
{
if (pm.response.json().arrayName[loop].arrayElement=== pm.variables.get(“arrayElementValue”)){
result=true;
break;
}
}
pm.expect(true).to.eql(result);
});

11. Verify whether the array is Empty

Also you can check whether array does not contain any values.

pm.test(“Empty Array”, function(){
pm.expect([2]).to.be.an(‘array’).that.is.empty;
});

These are the mostly used assertions in API tests. You can develop your tests based on any type of above assertions.

Are you still thinking that you missed the train? Rather trying to catch the passed train, take the right one which will take you to the right path!

Your tickets are ready in the station!

Software Quality Assurance Lead fascinated in Test Automation. Storyteller and a technical writer.