A 3 step Automation guide for APIs

Karthik G
upday devs
Published in
3 min readAug 15, 2022

API testing is performed directly on the endpoints/message layer as they APIs don’t have a GUI. API testing is part of integration testing and effectively validates the logic of the build architecture.

Photo by Markus Spiske on Unsplash

API testing is mainly focused to check the functionality, reliability, performance, and security of the programming interfaces. In API Testing, instead of using standard user inputs and outputs, we send calls to the API, get output and analyze the system’s response.

There are three separate layers in a typical app: the presentation layer (UI), the business layer, and the database layer for modelling data. API testing is performed at the most critical layer ~ business, in which business logic processing is carried out and all transactions between the UI and database layers happen.

In this blog, we elaborate on 3 key steps in creating an automation suite based on Postman, Newman and Docker.

Postman Custom Collection:

Postman is an API platform used to design, develop and test APIs. Postman is one of the widely used tools for API testing.

Please follow the below steps for creating a custom collection in Postman:

  1. Identify the APIs for testing, make sure all CRUD operations (POST/PUT/PATCH/GET/DELETE etc.,) are covered in the End-to-End flow
  2. Run the APIs individually and check for the expected response codes (200/201 etc.,)
  3. Create a new collection and add the APIs
  4. Navigate to the Tests tab of the API and add assertions, parsing depending on the use case (few examples below)
Illustration: Test setup to validate response code
//Checking for particular values in the response bodypm.test("Person is Jane", () => {
const responseJson = pm.response.json();
pm.expect(responseJson.name).to.eql("Jane");
pm.expect(responseJson.age).to.eql(23);
});

5. Export the collection as JSON file

Note: We can also export/share the collection as a shareable link but this is not recommended as there are no access controls.

Illustration: Exporting a JSON collection

Newman Setup & Running:

Newman is a command-line-interface (CLI) collection runner for Postman. It enables testing a Postman collection directly from the command line.

Newman resides on the NPM registry. To install Newman, follow the below steps,

NPM: Install Newman globally on your system allowing you to run it from anywhere. If you want to install it locally, remove the -g flag

$ npm install -g newman

Homebrew: Install Newman globally on your system using Homebrew

$ brew install newman

Newman CLI:

The newman run command allows you to specify a collection/URL to be run

$ newman run project/collection.json
$ newman run https://www.getpostman.com/collections/abcd-1234

Newman-Docker:

From the previous steps, we have created a Postman collection and setup Newman to execute the collection in CLI mode.

In this section, we will use Docker image of Newman to execute our collection following the below steps, which can be easily integrated into any CI tool to enable continuous testing of API collection

  1. Pull the newman docker image from docker hub:
docker pull postman/newman;

2. Run newman commands on the image:

docker run -t postman/newman run project/collection.json

docker-compose

We can also run the collection via docker-compose.ymlfile where we have to define the services that are needed for our app so that they can be run together.

docker-compose.ymlis a great option for CI/CD pipeline where we can run our collection without installing local dependencies and it contains build instructions for docker container.

Please follow the below steps,

  1. Create docker-compose.yml in the project directory with the following configuration,
version: "3"
services:
postman_collection:
container_name: postman_docker
build:
image: postman_docker
command:
run project/collection.json
--folder "Project Path"
--reporter-html-export project/sample.html
volumes:
- ./src:/etc/newman

2. Rundocker-compose up to run docker container

To conclude, in this blog post we glanced over the key steps in creating an automation suite using Postman-Newman-Docker along with examples and execution steps. I hope you enjoyed reading this article, and you know the drill — clap, comment and share.

Cheers, Karthik

--

--

Karthik G
upday devs

Work hard. Have fun. Dream big. Be adventurous.