End to End Testing with Postman and Docker ( Part 3 )

Ridho Perdana
Kurio Toolbox
Published in
5 min readJul 30, 2020

--

Run your End to End Test easily with 1 command!

Photo by Kelly Sikkema on Unsplash

In these blog series about writing End to End tests, we have talked about 2 things:

  1. Postman and Docker can be used as tools in Part 1.
  2. Writing a test script with Postman Collection in Part 2.

And for this final part, we will use the Docker to wrap the Application and the Test so it can be run everywhere easily. To make things shorter, we will assume that all of us already know about Docker, especially Dockerfile and docker-compose.

Exporting Postman Collection Script

At the previous tutorial, we already write a Postman script that can be run from Postman’s Collection Runner. Normally, to run the test we need to open Postman, accessing the Collection menu, and then run the test. But we don’t want to do it.

Luckily, Postman has its own CLI Application that can be run from the command line, Newman. If an App can be opened via command line, then it can be run automatically.

Postman’s Newman

To use Newman, we just need to export our Collection script from the Postman. Just click the 3 dots button at the collection’s list, and choose export.

Export location
Export menu

After exporting the data, we will get a JSON file that can be run with Newman by typing:

newman run <collection_name.json>

If all of our tests are successful, then the program will return 0, otherwise, it’ll return 1.

Test result with newman

Dockerize the Application (and the test)

One of Docker functionality is, we can run an application with its dependencies without installing it inside the host machine. That is the reason we can use Docker to run our Application that needs MongoDB and Newman to test without installing it inside our machine.

To dockerize our Application, here are the steps:

1.Create the Dockerfile for the Application. This Dockerfile will make an image that can become a container of our Application:

That Dockerfile will make an image that runs the application with its HTTP server. It contains a multi-stage build Docker. The first container will copy and build the code into binary, and the second will run the HTTP server.

2. Create a Dockerfile for our Newman Application. Actually, the Newman can also be installed at the previous Dockerfile, but that is a bad choice because it will make our Application’s Dockerfile become bloated with something that it shouldn’t need to care.

The Newman Dockerfile is simple, it will use node image because the Newman CLI is downloadable from npm repositories. This image also prepare the default Command for running our test.

3. Because we have multiple Dockerfiles, we need to separate the files into their own folder. We’ll create a deployment folder for the Application’s Dockerfile and test folder for the Newman’s Dockerfile. Remember our exported Postman script? Put that file inside the test folder too.

application
|
|- deployment
|--- Dockerfile
|- test
|--- Dockerfile
|--- postman.json
|- Dockerfile

4. Write a docker-compose file to wrap the Dockerfiles so they’re connected. Don’t forget to include a mongo service for our Application. The Dockerfile will be like this:

Inside that Dockerfile, we need to add some configurations, they are:

  • 3 Mongo environments, MONGO_INITDB_ROOT_USERNAME, MONGO_INITDB_ROOT_PASSWORD, MONGO_INITDB_DATABASE. This 3 env is needed for our Application so it can use the mongo service.
  • Mount a bash file (mongo.sh) inside the mongo service. This file is used to migrate our database. Although mongo can be used without a migration, we still need to create a user and database that can be used by our application. Below is the content of the file:
  • Mount the ./test directory from our host machine, so our test can access the postman.json file.
  • Declare a .env file that will be used by our Application.

4. Create a Makefile that can run our docker-compose and automatically kill it when the test is done. This is optional, if your team member can remember the command and flag of the docker-compose you don’t need to make this file.

Running the Test

After we create all the required files, we can run our test from the Host machine, which is our machine.

make test

The Makefile will run a docker-compose command that will automatically spawn a mongo, our application, and our test container. If the test container return 0, that means all of our tests are successful.

Running the docker-compose

That’s it.

Now we’ll have more confidence in our Application because of the End to End testing. And the best part of it, that it can run automatically, everywhere and by anyone.

You can see all of the Application’s code from below repository:

Thank you, and have a nice day!

--

--