top of page
Writer's pictureSHANTHI PRIYA

Environments And Variables In Postman

Introduction:

In this blog we will see what is the need of Environments and Variables and how to use them in Postman.

In real time we will be having a QA environment where we test all our APIs and move our code to the UAT environment. Base URL will get changed as QA will point to one server and UAT to another server. Before going to the theory part lets see few practical examples first as it will be easier to understand the definitions. Environments allow us to define a set of variables that belong to a certain context.

Since I cannot expose the APIs which I worked on in my company one can try out with many free APIs available in GitHub for practice. The sample API which I have used for practice is https://swapi.dev/api


How to use Environments and Variables in Postman



In the above screenshot of Postman, I have marked the Environments field in blue color and the sample collections which I created in red color for a better clarity for the beginners. When Environments tab is clicked, we can see the plus sign in the top left of our postman screen. This plus sign is for creating a new environment.



After clicking the plus symbol a new environment tab gets created which I have marked in green color. This can be named based on the requirements of your project and the environment where we will be testing our APIs.. For demo purpose have named one as QA and another one as Json placeholder Env. The yellow color marked area is where we set our variable and the initial value.



Under variables have entered base_url and set the initial value as https://swapi.dev/api/. Like this we can create many variables and use them in our pre-request script and tests dynamically. After entering the value click save. The current value will get assigned automatically during run time.



Now instead of giving the entire base URL we can change it to the variable name which we set in the environment Json placeholder Env. The format will be like this with double curly braces{{base_url}}. I have set the variable name as base_url. Any name can be given. If we hover our mouse we can see the variable initial value set and the scope of the variable whether set in global or collection level. This I have discussed below. We must make sure before we run our tests if appropriate Environment is selected. The red color indicates that. Some times the Test lead might ask us to run our tests in QA or any environment as required.



The above screenshot shows the initial, current and the scope of the variable when the mouse is hovered on the base_url.



Variable Scopes


Now after seeing basic practical examples, we can understand the theory part very well.

  • As said in the beginning of this blog Environment is a set of variables one can use in your postman requests.

  • Variables allows us to store and re-use values in our scripts and requests.

  • By storing a value in a variable as we saw above one can reference it throughout the collections, environments, and requests, and if we need to update the value, we only have to change it at one place.

  • Using these variables, it increases our ability to work efficiently and minimizes the likelihood of error.

Postman supports the following variable scopes:

· Global

· Collection

· Environment

· Data

· Local


Global- Global variables are available to all requests available in the Postman console regardless of which collection they belong to. These variables are general purpose variables and, as a best practice, should mostly be avoided: otherwise, you’ll get a crowded global space and you won’t know which variable belongs to which API.


Environment- Environment variables have narrower scope than global variables. If your collection needs to run against different servers, such as localhost, test server, pre-prod and prod, you can easily switch environments. Even if you have one server / API only, it might still be useful to use environment variables as it would keep the variables away from global namespace otherwise it can get crowded. In this case, however, you can choose to use collection variables too but environment variables give you different access levels when working as a team. You can assign the same role in the workspace or configure roles, such as viewer, editor etc. on an individual basis.

Environment variables should be used for environment specific information such as URLs, username and password.


Collection- A collection is a group of Postman requests that are related to each other. Collection variables are used to define variables at - guess where - the collection scope. Collection variables are available throughout the requests in a collection and are independent of environments.


Local- Local variables are temporary and only accessible in your request scripts. Local variable values are scoped to a single request or collection run, and are no longer available when the run is complete.


Data- Data variables come in the context of request execution through the collection runner. Postman allows us to execute requests in a collection through the collection runner and we can provide a data set in the form of JSON or CSV that are used while running the requests inside the collection so the source of data variables is the user-supplied data file in the format of JSON or CSV. During the request execution, the data variables can only be fetched but not updated/modified or added.


Postman official document says if more than one variable with the same name is available to a request, Postman will use the value from the variable with narrowest scope. This means that if you have an environment variable with the same name as a collection or global variable, Postman will use the environment variable, but local and data variable values will supersede environment values. The value of any overridden variables will display with a strikethrough. To know more about Postman, we can refer their official site Overview | Postman Learning Center which has many helpful documentations.


Summary


Postman environments are a place where you can create and manage variables. You can

set up multiple environments for different purposes. Environments are very helpful when

you need to share data between requests. If you are beginner go through the postman official documentation before watching any You tube or reading any blogs. Happy API Testing!




1,074 views

Recent Posts

See All
bottom of page