Creating Powerful Tests with Postman

Begum Gezer
Beyn Technology
Published in
8 min readJan 28, 2022

Postman provides JavaScript APIs that you can use in your request scripts. Postman PM library gives you the opportunity to create powerful test cases. The PM object provides numerous functionality for testing request and response data for your API test automation.

The PM Object

You can use Javascript language to write your test scripts in the Postman Sandbox which is a JavaScript execution environment that is provided to you while writing pre-request scripts and test scripts for requests.

To have powerful tests, the first thing you should do is start to parameterize your variables. You can access and change the value of variables with test scripts in Postman by using “pm” object.

You may use those variables in your test scripts while parsing JSON responses or making assertions.

Postman supports accessing variables by using “pm” object. The “pm” objects provide methods to access and manipulate global and environment variables.

Using Environment Variables in Test Scripts

You can create an environment to keep your variables where ever you want to access them in your requests. You can click on the view icon and create a new environment by clicking on the add button. You can name your environment and add variables.

You can edit your environment as you wish. You can change values manually or manipulate them by scripts. It’s all up to you how you want to name your variable and give an initial value if you need it. The current value will be changed when you manipulate variables by scripts and if you don’t have any current value, it will use the initial value of your variable when you call it by using “pm” object. I recommend you to add an initial value to variables where it will be recognized as null if you don’t assign it.

You can access those variables in your requests where ever you want. To access your variable as a request variable in headers, body parameter, or url extension, you need to use variable like {{variableName}} to access its value. This will return you the value of your variable in the active environment.

In your test scripts, you can use the method “pm.environment” to access and manipulate variables in the active environment. To activate the environment the only thing you should do is to choose your environment from the environment selectbox.

Here you can find a few useful methods of “pm.environment” to use in your test:

  • Check if the environment has the variable with the specified name:
  • Get the variable with the specified name in the active environment:
  • Set the variable with the specified name and value in the active environment:

Those three methods will be more than enough to create your test scripts, but you can search for further methods from Postman documentation specifically for your test purposes.

Parsing JSON Response

If you want to add assertions related to the API responses you need to parse your response body in the first place. When you have a JSON response to your API request, you can use the method JSON.pase(responseBody) to access the data of your response and extract value from the JSON object or you can prefer pm object to parse JSON response.

To be able to parse the response body, you should know about the structure of JSON format in the API response. It depends on how API is built to return which data.

Let’s first talk about what is JSON?

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate.

JSON is built on two structures:

  • A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
  • An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.

You can use JSON object and parse method like below to access responsebody of your request:

Another method is using pm object. You can also parse JSON response by using the following method:

Assume you have a JSON response in the following structure

When you know the structure, it is easy to extract values, just you need to traverse the same path as JSON structure for the desired key to extract value.

For example, if you want to extract firstname value, you should call the key as follows:

If you are trying to extract a value from an inner object in response, you can access the value like:

You may have more complex structures like inner arrays for your JSON format of the response. Methodology always is the same to access the value and follow the same path.

If you have multiple data in a key, you need other ways to extract the needed value.

Let’s say we have a response like

You can extract the key object first and you will have an array object containing player objects.

You can use the index to access a player object in the “players” array.

Debugging with Postman Console

You can use “console” methods for debugging your scripts.

Generally, we use debugging for two reasons:

  • To make sure that code executing in the right order.
  • To inspect the values of variables at a certain moment in the time.

How to log a message in the console:

  • First, open the Postman console (CTRL + ALT + C or click to console tab at left most bottom).
  • Under the Pre-request Script or Tests tabs, write your log something like

console.log(“I’m here…logging”)

  • Send the request and check the Postman console

Here are some console message samples:

When you send the request you see the output of your logs at the console like the following,

Adding Assertions

Adding assertions is the key to having powerful tests in any automation tool. While testing API requests you need to make assertions on one of the HTTP status code, response, headers, cookies, response time, or multiple checks in your tests.

You can add an assertion for the HTTP status code to check if your API request is successful as follows:

You can also check for other HTTP status codes and add messages like;

Another useful assertion you will need most in your test is response value assertions against a variable. You can add an assertion to check if a response property has the same value as a variable.

In the following example we are using an environment variable and use pm.environment.get(“variablename”) method to access its value. We are checking if the value of response property “name” is equal to the value of environment variable “name”.

Let’s have another example from a specific and a little more complex response and use string value to make an assertion instead of an environment variable.

I have the following response body and “players” array object in “data” property. The player object has the properties for id, username, currency, and external_id.

First, we should parse the response, extract the player object to access its properties, and make assertions.

We have the player object now. We can access its properties and add an assertion.

We are printing the message in which you will identify what is the result of your assertion before function definition for “pm.expect()”. And “pm.expect()” method allows you to compare values that you want to perform assertions.

After you finish adding assertions for your test script click to send for your API request. Open the Test Results tab to see your test results.

If your assertion is successful you will see your test status as PASS. If assertions are not correct then your test result status will be FAIL.

Let’s make a false assertion to see the test result.

In this case, your test will fail and you will see the status of your assertion as FAIL at Test Results.

You can find more assertion techniques according to your test case in postman learning center documentation.

This is the way how you create powerful API test automation in Postman. You can use your development skills to build even better test scripts. Hope you find this article useful and helps you to create better API tests in the future. In the next article, I will show you how to monitor your test automation and how to integrate it into your CI/CD process.

--

--