What is integration testing?
An integration test, as the name suggests, is a test that examines 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.
What is Docker?
It is a tool to design and facilitate the creation, implementation, and execution of applications using containers. This is where we place the software and all its dependencies with the ultimate goal of facilitating its distribution.
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 it may have. That also ensures that it can differentiate from the machine used to develop it from the machine that it is actually run on, and test the code.
Imagine that you have a project that is a Web API. With the ASP.NET Core, your main challenge is to perform integration tests on the project to ensure that a given HTTP request shows the expected behaviour. That is, returns the header, body, and status code you want.
So, how do you 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? This is where Docker comes in.
How Docker solves the problem
Docker makes the test execution environment more portable without impacting the computer. In this way, we encapsulate the environment with all the dependencies that are required by the project to run. In the event of a failure, we can disregard environment-related issues and focus on the source code.
We demonstrate the usage of Docker in a project developed in ASP.net core that has a proposal of integration test.
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. These 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.
Now that we have prepared the scenario, let’s go to the coolest part. The part where we 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 to configure your application’s services. In our case, the file we are using is docker-compose-integration.yml.
After that, with just a single command, 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 system configuration.
Remember that this installation only occurs once. So, the next time that this same command is executed, you will not need to perform all installations.
When the installation process finishes the tests, they will run on a screen similar to one 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.