I have followed the following article on getting my unit tests working in a Docker image and publishing via Azure DevOps pipeline.
Running Your Unit Tests With Visual Studio Team Services and Docker Compose
Each one of my unit tests projects have a very basic Dockerfile:
First Dockerfile for Application Tests
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
COPY . /app
WORKDIR /app/Application.Tests
RUN dotnet restore
Second DockerFile for Infrastructure tests.
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
COPY . /app
WORKDIR /app/Infrastructure.Tests
RUN dotnet restore
My docker-compose for both images:
application.tests:
image: ${DOCKER_REGISTRY-}applicationtests
build:
context: .
dockerfile: Application.Tests/Dockerfile
entrypoint: >
dotnet test --results-directory /testresults --logger trx
/p:CollectCoverage=true
/p:CoverletOutputFormat=cobertura
/p:CoverletOutput=/testresults/Application.Tests.cobertura.xml
/p:Exclude="[xunit.*]"
volumes:
- /opt/vsts/work/_temp:/testresults
infrastructure.tests:
image: ${DOCKER_REGISTRY-}infrastructuretests
build:
context: .
dockerfile: Infrastructure.Tests/Dockerfile
entrypoint: >
dotnet test --results-directory /testresults --logger trx
/p:CollectCoverage=true
/p:CoverletOutputFormat=cobertura
/p:CoverletOutput=/testresults/Infrastructure.Tests.cobertura.xml
/p:Exclude="[xunit.*]"
volumes:
- /opt/vsts/work/_temp:/testresults
Is there a way to combine the definition for each image in docker-compose? I understand that I can combine the tests into a single project, but want to maintain a 1:1 relationship between project and test project.
I should also add that both projects are in the same solution (.NET Core).