APIs Test Automation in 3 Easy Steps

Published Aug 13, 2017Last updated Feb 08, 2018

Introduction - Problem statement

Software APIs has been one of the most trending technologies used recently. Great web applications and leading mobile apps are all the results of greatest API at the back-end. This of course bring to the table an even greatest QAs who are willing to test those APIs in an efficient way and to always make sure they are running as expected.

To achieve all of the above, in an agile product development life cycle (PDLC), we - as QAs - need the easiest and the fastest way to test and to automate our APIs. We even want to be able to monitor those APIs health on production.

This article is for all who loves to develop their skills set. However, I see it's for

Level: Intermediate || Reading Duration: 30 minutes

Solution Summary

In the coming few easy points, you will learn how to test, automate, and monitor your product API endpoints in very easy 3 steps. This might not be the best solution, but I can guarantee it is the fastest.

We are going to use the following tools & technologies:

Postman, as our API automation scripting tool.

Newman, to make it possible to run the tests via simple command line.

Since I will not be able to cover everything in one article, I will post advanced tutorial as a part 2 of this one that will cover:

A few lines of JavaScript to write our tests against the API responses.

So! Let's do the 3 easy steps

Step 1: Setup your Postman Environment Files

Download Postman and install it on your machine (www.getpostman.com).

Signup. Don’t worry, it’s free.

Setup your first environment config as following:

Click on settings icon on the top right, then manage environments

Create a new environment by clicking "Add" button, let's call it Dev Test (Assuming that we want to start testing on Dev environment).

Add key "host" and value as your test environment URL. In our case I will use (https://www.baaz.com/api/1.0). Actually, this is our production APIs URL.

Now save it, and let's create our first test

Step 2: Create your First Test & Run It

Now we are going to create our first API test for login to Baaz and get the logged in user data. It is very important that you understand the differences between API call types. As a summary, the most used types are as following:

POST: for endpoints that insert data.

PUT: for endpoints that update existing data.

GET: for endpoints that retrieve data.

DELETE: for endpoints that delete data.

Alright! But before we start, let's have a look at what Postman GUI looks like and get used to a few definitions:

Request Body: where you send your requests body as what we will see next in our test. We send username & password data with the request.

Response Panel: here you see everything related to the response resulting from our request. You will see response body, cookies, headers, and your test results.

Response Information: general info such as the response time, size and code.

Environment: Remember the Dev environment we setup, here you choose it before sending your request. Again, we might have several environments defined here. For example: integration, staging, and production environments. Each one has its own host URL, and its own test data (users, passwords, etc..).

So we'll create it now

Start with making a new Postman collection by clicking "Collection" from the main menu, then "New Collection".

Give your collection a name and a description.

Now add a new request to your collection, by clicking "File > New Tab", or just click on the "+" sign.

The empty tab you got is where our API request will start. Now choose your request type. It's "POST" in our case. Then write your API request URL. In our case it is

{{host}}/tokens

I guess you already knew why we used the curly brackets. If we want to use any environment variable in request URL or request body, we simply call that variable name inside these curly brackets.

It's the time now to write our request body. In our case, we want to send the username and password with our request to get the access token. So, the body request is something like:

Guess what! the username & password, we already added them to our environment variables. So now, if you want your test to be running on different environment such as staging, all you have to do is to create a new environment config, add its host, username, and password. Then run your tests. This way, you will be able to run the same designed test on as many environments as you want with almost no time.

Don't worry about other values we sent with the request, those are fixed and related to our login functionality.

It's time to write some tests against our API response before we run the test. So navigate to tests tab and write the following 2 lines of code:

The first test is to test that response code is 200, which means OK. The second line is to test the response time does not exceed 2 seconds.

You can find more test types and syntax here: (https://www.getpostman.com/docs/testing_examples).

Let's hit "Send" button and check out the results:

As you see, the response body on the bottom part is JSON format containing the user access token and other data. If you click on test tabs (on both top & bottom parts), you will see the tests we wrote on top, and their results on the bottom. Check the following screenshot:

Oh! You noticed the 2 extra lines of code? Well, this is how we store specific values from our JSON response body into environment variables. We do so in order to use those values later in other requests. This is called "Chaining Requests" in the API automation world

The 1st line, is defining a variable called jsonData and storing the whole response body in it. While the second line is storing the value of access_token into a new environment variable called access_token1.

As a final step, save your request by clicking on save, then choose which collection you want to save it in.

Now you can click on "Runner" and run your tests from GUI. Simply, choose the test collection, specify the environment then hit "Start Run".

However, this is not the best practice, since you will have many API requests in many collections and it will be hard to run them one by one. In addition, you would need the command line run if you are going to integrate your tests with CI tools such as Jenkins, CircleCI, or TeamCity - which we will cover in the next part of this tutorial.

Step 3: Run tests from command line

We are done with the first test. We will need to run it from command line now. So here's how:

Download & Install Newman.

To run Newman, ensure that you have NodeJS >= v4. A copy of the NodeJS installable can be downloaded from https://nodejs.org/en/download/package-manager.

The easiest way to install Newman is using NPM. If you have NodeJS installed, it is most likely that you have NPM installed as well.

Open you command line (CMD) and write the following line:

npm install newman --global;

To make sure Newman is installed correctly, write "newman --version" in your cmd and hit "Enter". You should get your Newman version. In our case it should be something like 3.1.0

Now, we would need to export our test collection and environment files. Save them on your local drive as following:

Export Test Collections

Then:

Export Environment file

Make sure collection file has the extension ".postman_collection" and environment file has "postman_environment"

Let's run our test collection from command line. Write the following line:

So, we specified our test collection file and the environment file to be used during the run. Results would be something like below:

More command options? Such as reports? Check Newman documentation here:

https://www.npmjs.com/package/newman

Where to go next

Now, you have really great knowledge on how to start your API test automation. Yet, you need to understand how tests scripts should go with the development code itself from a branch to another, and how to automate tests in Agile & Scrum development process within sprints in parallel with development in order to achieve the "term" continuous delivery. That's why in the next article I will show you: