API Testing — Introduction to Variables types, scope, and the usage

Twinklemehta
7 min readJan 4, 2021

What are Variables?

Variables are like boxes where you can store the data and reuse them when needed. Using variables makes it easier to reference information in the program.

For example, instead of typing “Computer Science Instruction” over and over again, we can instead make a variable called “x” to store the word, and when we want to use it all you have to do is type “x”!

Variable Usage in POSTMAN

Variables are any elements or data store that can take different values. These are referred to as key-value pairs in POSTMAN. Each variable name represents its key, so referring to the variable name allows you to access its value.

For example, if you have a base URL for a request stored in a variable named base_url, you can refer it in your requests using {{base_url}}.

By storing a value in a variable, you can refer it throughout your collections, environments, and requests and in case, you need to update the value, you only have to change it in one place. The efficiency of your work increases if you use variables and it minimizes the likelihood of errors.

Types of Variables

POSTMAN supports different types of variables for performing API Testing.

What are the global variables?

Global variables are environment independent variables and are available to all the requests present in the POSTMAN console regardless of which collection they belong to.

For Example, if you have 3 global variables like G1, G2, G3 and they have respective values of the name of the students then everywhere whenever you are using the postman, you have different collections or you can have a different request, you can use these global variables in your test cases by using double curly-braces “{{ }}” by providing the name of the key variable and it will be replaced with the respective dynamic value.

Scope of Global variables in POSTMAN

A global variable can work outside the environment also and does not require any environment to be selected as these are general-purpose variables.

To create a global variable, click on the eye icon displayed next to the environment dropdown, a context window will be opened with details around the global variables.

When you click on the edit option, you can create a new global variable by entering the name and an initial value.

For example, I have created a global variable using a dummy API as depicted below. I have taken the Global variable name as “LoginURL” and have provided its initial and current value. After hitting Save, my global variable gets created.

Now, when you want to hit the Login request, you neither have to select any specific environment nor have to type the request manually. Just provide the global variable name “{{LoginURL}}” by selecting the POST operation and the valid credentials in the BODY of the request.

After hitting the Send button, the token gets generated in the response with the status code of 200 — OK.

To get more clarity, you can see that while hovering over the variable name provided in the request section: G symbol is displayed that indicates, the API is global and can be used anywhere over the workspace.

What are Collection Variables?

Collection variables are available throughout the request in that particular collection. They are independent of the environment, so it does not change based on that selective environment.

For Example, you have three different collections named “Users collection”, “student collection” and “Login collection”. If I want to have a particular URL, specific to any collection I can do that using collection variables.

Scope of Collection Variables in POSTMAN

Collection variables are most suitable if you are using them in a single environment. Collection variables can be added while creating a collection or it can also be added while editing an existing collection. For creating or editing a variable for an existing collection, select a collection in Collections on the left of the Postman app, open the View more actions () menu, and click Edit.

Provide Key-value under the Variables tab at the collection level and enter its initial value. For example, I have created a variable (as depicted in the below screenshot) named “emp” with the initial value “employees” http://dummy.restapiexample.com/api/v1/employees).

After hitting Update, we can use this collection variable in our request at the collection level. Now, when you want to hit the GET request with the endpoint “employees”, you just need to provide the collection variable name “{{emp}}”. After hitting the Send button, the response is received with the status code of 200 — OK.

To get more clarity, you can see that while hovering over the variable name provided in the request section: C symbol is displayed that indicates, that this is a collection variable and can be used at the collection level.

What are the Environment Variables?

In Postman the environment variables are a set of key and value pairs. For differentiating between the request’s environment play a crucial role. Changing the value of the key-value pair under environments automatically reflects the changes in our requests.

Environment variables allow us to customize our processing to different environments, for example, local development vs testing or production. Only one environment can be active at one time.

Scope of Environment Variables In POSTMAN

We use Postman to create an Environment and Environment Variable and it is very easy to do that, but it has three steps involved in the process:

1. Create an Environment

2. Create Environment Variables

3. Use an Environment Variable in the Request

When you click on the Gear icon, you can create a new environment variable by clicking on the Add button.

1. Provide the name of the Environment, for example, I named it a TEST and have created an environment variable by providing the key value with the initial and current value.

2. When you Hit Save, my environment plus environment variable (LoginUrl) gets created.

Now, when you want to hit the Login request, you just have to use an environment variable under the specific environment which is “TEST” instead of entering the whole request manually. Provide the environment variable name “{{LoginURL}}” by selecting the POST operation and the valid credentials in the BODY of the request.

After hitting the Send button, the token gets generated in the response with the status code of 200 — OK.

To get more clarity, you can see that while hovering over the variable name provided in the request section: E symbol is displayed that indicates, the API is environment-specific and can be used on the environment level.

What are Local Variables?

Local variables are temporary values that you set in your request scripts using the following syntax:

Scope of Local Variables in POSTMAN

Local variables do not persist between sessions, but allow us to override all other scopes temporarily, during the execution of a request or collection/monitor run. For example, if we need to process a temporary test value for a single request or collection run locally, and don’t want the value to sync with your team or remain available when the request/collection has finished running, we can use a local variable.

What are Data Variables?

The data variables can be used using a collection runner where we can import CSV and or a JSON file in order to provide the values for an API request. We cannot set a data variable inside POSTMAN, so that is why the functionality was made to import the file from outside to make it dynamic so that the values can be changed outside and the file can be imported.

Scope of Data Variables in POSTMAN

We can use data files to pass Postman sets of values to use in a collection run. By selecting a JSON or CSV data file in the Collection Runner, we can test your requests with multiple different values as part of a single run. When we initiate a collection run, we will see an option to select a data file.

1. Click Runner at the top left of Postman. Select your collection and click Select File available next to Data.

2. Import a CSV JSON file in the Select File option.

3. You will see an option to Preview the data in the file before starting the run.

4. Click Run Demo to begin the run with the values from the file.

Conclusion:

Postman variables allow us to store and reuse values in your requests and scripts in an effective way. Whatever variables we use and how we use them can benefit your testing by knowing which scope to use and when to provide a more readable and logical way of accessing data.

--

--

Twinklemehta

Software Quality Assurance Engineer with 3 years of Experience in Information and Services Industry.