API Testing with Postman and Newman

Did you know that Postman can be used to automate API Calls? Also, there is a brother of Postman, called Newman, which can run the same automates from the command line. Let's see this thing in action.

Download:

Please fork the complete project from my Github.

Postman Environment variables

You may want to run your automates with a different set of data, based on region, environment etc. In this case, you can create your script to read input value from environment variables, so that these values are externalized.

Postman ENV variables

Update: (23/02/2020)

NOTE:

It is always, good to follow a naming convention for your Collection ENV. Else, it becomes a big mess.

I prefer, the below naming

<PROJECT_NAME>_<REGION>_<QA/DEV/PROD/LOCAL>

Postman Foldering

It is better to group APIs into folders. I have a Project Collection and subfolder for each group of APIs.

Order the requests in this order,

CREATE → FETCH → UPDATE → FETCH (optional) → DELETE

Postman Folder

Postman Requests:

We need to make the requests generic, so that, it can work with different env.

Postman request

If you see, the baseURL is an environment variable. It can change between QA, DEV, PROD etc.

Also, the JSON payload is receiving data from environment variables.

NOTE:

  • UUID

There are scenarios when you need to send UUID from the front-end. In this case, you use $guidin the BODY. Please find the usage below

Postman Test

In the previous scenario, we saw Create Article Request. We need to validate article creation when the script executes. Also, we need to use the same article_id in the subsequent requests (UPDATE, GET, DELETE).

So we write Post Execution Actions.

Postman Test

Here, you can see

  1. We are copying request.id value into article_id env variable.
  2. Validating the status code returned
  3. Printing the article_id, in the output. (This is very useful when you are running the script from cmd)

Btw, pm stands for Postman, which is a global object. It is similar to windowobject in browser.

In the subsequent request, we can use the article_id as shown below.

Get Request

Postman Collection Runner

Now comes the execution part. You can execute the collection using collection runner.

Initiate Collection Runner

Execution:

Select Requests to execute

Note:

There are scenarios, where you don’t want to perform particular requests, say DELETE (since it will be used in subsequent calls). In that case, you can untick those requests.

The execution result looks something like the below:

Execution Report

You can see, the success and failure in the end report.

Postman Export

You can export your collection and environment.

Export Collection
Export Collection ENV

Newman (The CLI runner)

There are scenarios when you need to run this API scripts on a Linux instance. In my case, I wanted to run these scripts in a Linux Bastion machine.

For me, the best option was to use Newman.

The above commands install Newman in your Linux machine.

Now copy the collection and environment backup into Linux instance. You can use FileZilla for the same if you plan to run it on EC2.

You can execute the automate using the below command.

Note: Here -e means, use the environment file that follows.

You can see the newman execution happening the cmd.

The result would look something like below

newman output

Note:

As discussed earlier, we can print id of the resource created in the output for debugging purpose.

Hurray, you have learned API automation using Postman and Newman.

Found it Interesting?
Please show your support by 👏.

Writes on Big Data, AWS & Backend technologies.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store