API Test Cases in Postman using JavaScript

API Test Cases In Postman Using JavaScript

In the previous articles on Postman Tutorial, we have covered “How To Run Collections Using Newman

In this “API Test Cases in Postman using JavaScript” article, I will be demonstrating as to how you can implement this concept and get a tight grip over this.

With Postman, we can add scripts to our request and write tests.

Code added under the Pre-request Script tab will execute before the request is sent, and code added under the Tests tab will execute after the response is received.

Postman Prerequest Scripts Test Script

We use pre-request scripts for all the things we need to do before the execution of the request such as setting variables, clearing the variables, or set the environment variable in the environment.

We write Test scripts to validate API’s error handling by sending a request with incomplete data. Through Test scripts, we can use dynamic variables and can carry out test assertions on response data.

Postman Prerequest Scripts Test Scripts

Postman has a PM API (known as the pm.*API) which is the powerful way to write tests in Test tab.


The pm.test() function is used to write test specifications. It accepts 2 parameters, the name of the test (as a string) and a function to return a boolean value. It can be used only in the Tests tab after the primary Postman request has been sent.



The pm.expect() assertion function makes it easy to write readable tests, and we can deal with assertions of data from a response or variables.


pm.response.to.be.* :

The pm.resonse.to.be object provides tests for response status types and body variations.


Test Cases:

We need to add any of the assertions inside a pm.test callback.


1) Status Code:

1.1 Check if the status code is 200:

1.2 Checking multiple status codes:

Postman Status Code

2) Response time:

2.1 Response time below 100ms:

Postman Response Time

3) Headers:

3.1 Header exists:

3.2 Header has value:

Postman Header

4) Cookies:

4.1 Cookie exists:

4.2 Cookie has value:

Postman Cookies

5) Body (Any content-type / HTML responses):

5.1 Exact body match:

5.2 Partial body match/body contains:

Postman Body

6) Body (JSON responses):

Parse body (need for all assertions)

const response = pm.response.json();

6.1 Simple value check:

6.2 Nested value check:

Postman JSon Body

7) Body (XML responses):

Convert XML body to JSON (need for all assertions):

const response = xml2Json(responseBody);

7.1 Simple value check:

7.2 Nested value check:

Postman XML Body

Next steps:

Learn “Generate Random/Dynamic data in Requests” in the next tutorial.

Author Bio: This article is written by Harsha Mittal an ISTQB-CTFL Certified Software Test Engineer having 3.5+ years of experience in Software Testing.

Related posts:

Sharing is caring.

Share on facebook
Share on twitter
Share on linkedin

Like This Post?

We have a lot more where that came from?

We only send really good stuff occasionally, promise.

Rajkumar SM

Leave a Comment

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

Share on facebook
Share on google
Share on twitter
Share on linkedin
Share on whatsapp

Recent Posts:

Scroll to Top
API Testing eBook