Building microservices through Event Driven Architecture part10: Run Unit tests inside a docker container
This tutorial is the 10th part of a series : Building microservices through Event Driven Architecture.
The previous step is about building microservices through Event Driven Architecture part09: Handling updates : https://logcorner.com/building-microservices-through-event-driven-architecture-part10-handling-updates-and-deletes/
In this tutorial, I will show how to run unit tests inside a docker container.
Because I will run the micro services in containers using kubernetes, unit tests, integration tests, continuous integration and continuous delivery will be done with docker images and containers.
LogCorner.EduSync.Speech.Domain.UnitTest
Let us go ahead and open the LogCorner.EduSync.Speech.Domain.UnitTest project and create a docker file like this (you can generate the docker file by right clicking on the project name and select Docker Support)
Docker File
The Docker file should look like this :
- FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
use mcr.microsoft.com/dotnet/core/sdk:3.1-buster as base image
- WORKDIR /src
Create the /src directory in the docker image.
- COPY [“LogCorner.EduSync.Speech/LogCorner.EduSync.Speech.Domain.UnitTest/LogCorner.EduSync.Speech.Domain.UnitTest.csproj”, “LogCorner.EduSync.Speech/LogCorner.EduSync.Speech.Domain.UnitTest/”]
COPY [“LogCorner.EduSync.Speech/LogCorner.EduSync.Speech.Domain/LogCorner.EduSync.Speech.Domain.csproj”, “LogCorner.EduSync.Speech/LogCorner.EduSync.Speech.Domain/”]
Copy LogCorner.EduSync.Speech.Domain.UnitTest.csproj file and LogCorner.EduSync.Speech.Domain.csproj file on the current folder (src)
Note that referenced .csproj project files should copied in order to be able to restore packages later on.
- RUN dotnet restore “LogCorner.EduSync.Speech/LogCorner.EduSync.Speech.Domain.UnitTest/LogCorner.EduSync.Speech.Domain.UnitTest.csproj”:
Restore packages for the LogCorner.EduSync.Speech.Domain.UnitTest project and the referenced projects.
- COPY . .
Copy everything (except the files/directories included in the .dockerignore file) to the /src directory in the docker image.
- WORKDIR “/src/LogCorner.EduSync.Speech/LogCorner.EduSync.Speech.Domain.UnitTest”
Change the current folder to the LogCorner.EduSync.Speech.Domain.UnitTest project.
To test it, move to \\LogCorner.EduSync.Command\src folder and run the following command to build and create an image named unittest-image using the unite test Docker file :
docker build -t unittest-image -f LogCorner.EduSync.Speech\LogCorner.EduSync.Speech.Domain.UnitTest\Dockerfile .
Then, start an container in interactive mode using unittest-image as image
docker run -it unittest-image
Finally run unit test using the following command :
dotnet test –logger “trx;LogFileName=UnitTestResultfile.trx”
I can edit the test result file using the command
cat UnitTestResultfile.trx
docker-compose file
Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration. https://docs.docker.com/compose/
So let us create a docker-compose-unit-tests file
docker-compose-unit-tests file
This file is used to build and run an image logcorner-edusync-speech-domain-unit-test and it uses the unit test Dockerfile.
Then create and start the service image logcorner.edusync.speech.domain.unittest
docker-compose.override-unit-tests file
This file overrides the docker-compose-unit-tests file, SoI add a ASPNETCORE_ENVIRONMENT and a entrypoint to run the unit tests
Build the unit test project
To build the unit test project, I can run the following command :
docker-compose -f docker-compose-unit-tests.yml -f docker-compose.override-unit-tests.yml build
The build will create an image logcorner-edusync-speech-domain-unit-test:latest.
Let us run the image logcorner-edusync-speech-domain-unit-test:latest in interactive mode with the following command :
docker run –rm -it logcorner-edusync-speech-domain-unit-test:latest
ls -la
Let us run the unit tests
dotnet test –logger “trx;LogFileName=.\domain-tests\tests-results-domain.xml”
ls -la
cd TestResults
ls -la
cd domain-tests
ls
I should see a file inside the container named tests-results-domain.xml
I can edit the tests-results-domain.xml file with the following command.
cat tests-results-domain.xml
exit
Then If I start the container again , the tests-results-domain.xml file should disappear.
Start the container again
docker run –rm -it logcorner-edusync-speech-domain-unit-test:latest
ls -la
Publish tests results
Because the test result file (tests-results-domain.xml) disappear when the container stop running, I have to share tests-results-domain.xml file between the docker container and the host.
So I will create a folder on the host and mapp it to the folder that contains the tests results inside the docker container.
I have to add a volume like this in the docker-compose file :
volumes:
– ./TestResults/tests-results-domain:/domain-tests
And, I create a volume mapping between the volume domain-tests of the container and the volume tests-results-domain of the host.
volumes:
– ./tests-results-domain:/domain-tests
Run the unit test project
To run the unit tests, I have to run the following command :
docker-compose -f docker-compose-unit-tests.yml -f docker-compose.override-unit-tests.yml up
I should have a tests-results-domain folder created on the host and this folder should contains the tests results.
In my CI/CD pipeline I will use the TestResults folder on the host to publish tests results
Dockerizing the other UnitTest projects
To dockerize the other unit tests project ( infrastructure, presentation, application), I follow the same scenario as domain unit test
You can find here the entire docker-compose-unit-tests.yml and docker-compose.override-unit-tests.yml file
To build and run all unit tests , I have to run the following commands :
docker-compose -f docker-compose-unit-tests.yml -f docker-compose.override-unit-tests.yml build
docker-compose -f docker-compose-unit-tests.yml -f docker-compose.override-unit-tests.yml up
code source is available here :
https://github.com/logcorner/LogCorner.EduSync.Speech.Command/tree/Task/DockerUnitTesting
Regards
<a href=”https://www.buymeacoffee.com/logcorner” target=”_blank”><img src=”https://cdn.buymeacoffee.com/buttons/default-orange.png” alt=”Buy Me A Coffee” style=”height: 51px !important;width: 217px !important;” ></a>