How to use Docker in Integration Testing?-Integration testing with Docker
This post is about “How to use Docker in Integration Testing”
To convey some of the knowledge we learn in this journey. We create this post that we believe can be useful to other people who are in a similar scenario.
But before we begin, here’s a brief introduction to integration testing and Docker, okay? Then come with us
What is integration testing?
Briefly, an integration test, as its name says, is one that tests the integration between two parts of your system.
For example, the tests you write for your Web API, where your project performs HTTP requests, is an integration test. This means that you are testing the integration of your system with the external system, which is the API. Quiet so far? If you want to know even more about this, you can check this link here very cool from Microsoft 🙂
What is Docker?
It is a tool to design and facilitate the creation, implementation, and execution of applications using containers that are like boxes. Where we place the software and all its dependencies, with the ultimate goal of facilitating its distribution.
And it is thanks to this action of containers that the developer can be sure that the application will run on any other machine, regardless of any custom configuration that this machine may have and that can differentiate from the machine used to develop and test the code. And if you also want to know more about this subject, just access this link here.
Imagine that you have a project that is a Web API. We with the ASP.NET Core and your main challenge is to perform integration tests on that project to ensure that given an HTTP request it has the expected behavior, that is, returning the header, body, and status code you want.
How to create an environment where you can trust that all dependencies that the project needs are available without being influenced by the location where the project is running? At that moment the docker came into my life and filled her with joy.
The solution we found with Docker to solve this situation:
To make the test execution environment more portable, that is, without impacting the computer. Where the clone of the project can avoid that famous phrase”Ahhh, but it works on the machine”, so we can decide to use Docker.
In this way, we encapsulate the environment with all the dependencies. That requires the project to run, and in the event of a failure, we can “disregard” environment-related issues and focus on the source code.
The purpose of this post is to focus on the use of Docker, right? Therefore, below we demonstrate using a project developed in asp.net core that has a proposal of integration test and is available on this link, you can clone the repository using git, or download, as you prefer.
Within this repository, there are some Docker files that need to set the stage for our integration tests and they are:
It is a text file that contains a list of commands that the Docker daemon calls during the creation of an image and will be used to create and publish the content of the service and copy the content published in a container.
It will be used to create and restore the integration test project, preparing it to run the tests.
It will be used to maintain the All service and run our integration tests using the service.
Well, now that we have prepared the scenario, let’s go to the coolest part. Which is to execute the project. For this, we will use the docker-compose which is a tool to define and run Docker applications with several containers.
With Compose, you use a YAML file (YAML Ain’t Markup Language) to configure your application’s services (in our case the file we are using for this purpose is docker-compose-integration.yml).
Then, with just a single command (yes, that’s right), you create and start all the services in your configuration. Super awesome, right?
docker-compose -f docker-compose-integration.yml up
After executing the command you must have a screen similar to the one below that consists of executing the commands that are in the After executing the command you should have a screen similar to the one below that consists of executing the commands that are in the
Remembering that this installation only occurs once, that is, the next time that this same command is executed, you will not need to perform all installations.
When the installation process finishes the tests will run on a screen similar to this that contains some information between them whether the test was run, passed, failed, and the runtime.
And that’s it, people! We hope you enjoyed it and helped in the learning process about this wonderful world that is Docker. Oh, and of course, if you want to share your experience with Docker or have any questions about the explanations of this blog, leave your comment here that we will be very happy to answer.