Introduction
In this article, I will review the process of running our API test suite using a Docker container. The container contains Newman- the command line Collection Runner for Postman.
I assume that you are already familiar with Postman and maybe use Newman for your CI/CD process.
Tool advantages - Why should we use a container for testing?
In order for newman to run our test suite, the following installations are required:
The installations are time-consuming, and sometimes other versions of node / newman can cause the tests to be flaky or to fail completely.
To Avoid doing this installation over and over on our colleague’s machines / remote cloud machine and to assure that our testing environment is isolated docker comes to our rescue.
Docker installation & setting up the environment
First, we need to install the Docker community edition.
For windows, there are two alternatives:
- Docker official website - more usual information can be found here.
- Through Chocolatey in this link.
Hyper-V should be enabled - more info could be found here.
For Linux Ubuntu users:
Running our test suite
Now after everything is installed, run the following command from our test suite folder:
docker run -v {test_suite_folder}:/etc/postman -t postman/newman_ubuntu1404 \
--collection="API.json" \
--globals="globals.json" \
--environment="env.json" \
--reporters="junit" \
--reporter-junit-export="newman-report.xml"
Commands explanation:
- Docker now mounts our testing path to a docker internal path /etc/newman using the -v flag.
- We allocated a pseudo-tty to the container to execute the task which is newman_ubuntu1404 using the -t flag.
- Run test suite named “API.json” using the —collection flag.
- Global variables provide our tests with a set of variables that are available in all scopes. In this example we global file called “globals.json”, using the —globals flag.
- From postman website: An environment is a set of key-value pairs. The key represents the name of the variable, environments let you customize requests using variables so you can easily switch between different setups without changing your requests. The environment file called “env.json” and was provided using the —environment flag.
- Saved a report file in a JUnit format using the —reporters flag. This format is recommended because Jenkins has a plugin that displays JUnit reports, you can also use the “CLI” option to print the results to the console.
- Export the report to a file called “newman-report.xml” using the —reporter-junit-export flag.
In conclusion
In this article we reviewed running our automated API test suite using an existing docker container from Docker Hub, we started by installing docker on our machine, then run the command that executes our tests that were explained line by line. Now we can scale our tests to any environment (Amazon, Google Cloud, etc..); the only installation needed is Docker.
Happy testing!