Combining Unit Tests from Multiple Projects into One Docker Image for Azure DevOps Pipeline

Viewed 1021

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).

1 Answers

Is there a way to run the tests for all projects in a single azure-pipeline task?

It's not available with DockerCompose task. According to your scenario, you should avoid combining multiple containers into one image. As the answer in this thread pointed that images should be kept light, and run one service per container.

Docker Compose Task is just the way to manage these services with multiple containers to run in your application. .

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

If you insist on combing everything together, Docker compose is not fit for your case.

You could have a try with multiple stage dockerfile to combine these dockerfiles into one. This will end up with a single image when your run docker build

Related