API Testing Using Postman

API Testing Using Postman

API Testing Using Postman is one of the most convenient ways of backend testing these days.

Please find below the concepts of API Testing Using Postman.

How to create dummy API in your local machine for practice.

While practicing the postman tool we search for free APIs from Google and use them, these dummy APIs sometimes work and sometimes don’t work properly. So better than this we can create our own API in a local server, we can run those API anytime, we can send the request and get the response, and can do the practice.

In order to create dummy APIs, there are some prerequisites.

  • Install NodeJS: Download Link: https://nodejs.org/en/download
  • npm: npm comes with NodeJS, no need to install npm separately, we can check the npm version using the command
npm -version
  • Install json-server using the command
npm install -g json-server

Now we need to populate some data in the jsonserver because when we send the API requests it should return some response. So we will create a json file which will contain some data. Here in my case, I have created file named info.json.

json

Now execute the below command to make your API up and running. 

json-server info.json

Once we will execute the command it will give us the Resource URL and home URL. As soon as you get the URL you can now copy those URL and hit in the browser to test it is running or not. That’s it, now we have successfully created the dummy APIs and we can execute the HTTP requests like POST, PUT, GET and DELETE etc on this particular API.

REQUESTURI
GEThttp://localhost:3000/users
GEThttp://localhost:3000/users/1
POSThttp://localhost:3000/users
PUThttp://localhost:3000/users/4
DELETEhttp://localhost:3000/users/4

What are variables in postman?

Sometimes we need to use common data across multiple requests, so instead of specifying the data at multiple places and multiple times we can just create variables and we will refer to that variable name in every request. For example, in the above URI the URL is exactly the same in each request i.e “http://localhost:3000”, suppose tomorrow if URL got changed then we have to go and modify every request, but if we use a variable then instead of modifying every request we can simply modify the variable value that’s it.

In postman, we have two types of variables

  1. Collection Variables
  2. Environment Variables / Global Variables

Collection Variables

These variables we can use within the collection in all requests. We can define a variable for the URL “http://localhost:3000” and can use that variable in every request.

  • Select the Collection
  • Click to Edit
  • Go to variable tab and define a variable at the collection level and click on update
  • Once the variable is defined we can now use it inside the collection in every request, the syntax will be
{{variable name}}
variable
collection level variable
use variable
use variable

How to define Collection Variables using a script?

So instead of defining the variable in the variable tab as described above, there is one another way of defining the variable using postman statement.

  • Select the Collection
  • Click to Edit
  • Go to Pre-request Scripts and define a variable using postman statement:
    postman.setGlobalVariable("Variable Name","Value")
  • Click on update, Once the variable is defined we can now use it inside the collection in every request, syntax will be {{variable name}
variable as script
variable as script

Environment Variables / Global Variables

We can create this variable at the postman tool level and can use it across multiple collections throughout the postman.

  • Go to manage the environment
  • Click on Add 
  • Add Environment name, in this environment we can add multiple variables, all these variables will get associated with this environment.
  • Click Add
  • Now while running the collection in the collection runner selects the environment, so that all the variables we created under this environment will be automatically applied to all the requests of this collection.
environment
environment in postman
runner
select environment

What is the workflow and how we can control the flow of execution of the request in postman?

By default, all the requests will execute in the default order but if we want to change and control the order of execution we have to define that. 

  • Go to the request
  • Go to the test tab, here we have to define the next request that we want to execute, the syntax will be
postman.setNextRequest("Request Name");
request
define the next execution

 

CHECK NEXT PART

About Postman