Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run dotnet test inside docker container

I want to run the dotnet test command inside a docker container but I just cannot figure out where to put the command. The project is a .NET Core 2.1 test project. The reason for this is that I want to run end-to-end integration tests which require all my containers to be running.

DockerFile:

FROM microsoft/dotnet:2.1-runtime AS base
WORKDIR /app

FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY *.sln ./
COPY Sombra.IntegrationTests/Sombra.IntegrationTests.csproj Sombra.IntegrationTests/
COPY . .
WORKDIR /src/Sombra.IntegrationTests
RUN dotnet build -c Release -o /app

FROM build AS publish
RUN dotnet publish -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "test", "Sombra.IntegrationTests.csproj"]

docker-compose.yml

version: '3'

services:
  sombra.integrationtests:
    image: sombra.integrationtests
    build:
      context: .
      dockerfile: Sombra.IntegrationTests/Dockerfile
    depends_on:
      - rabbitmq
like image 571
JelleKerkstra Avatar asked Dec 17 '22 23:12

JelleKerkstra


2 Answers

You're using a runtime image to invoke the sdk command test. Your final is from base and base is from runtime.

I've successfully used unit tests as intermediate step while building the container but never integration tests with docker-compose. The key difference is I used a RUN command instead of the entrypoint/cmd so the tests are already executing while building the container. The main advantage is that there is no final image when the tests fail. But then again, this were pure unit tests and no integration tests. Although I can imagine that this will also work.

Here is my full example:

FROM microsoft/dotnet:2.0-sdk AS build-env
WORKDIR /app

# copy csproj and restore as distinct layers
COPY test.sln ./test.sln
COPY program/program.csproj ./program/program.csproj
COPY program.tests/program.tests.csproj ./program.tests/program.tests.csproj
RUN dotnet restore

# copy everything else and build
COPY . ./
RUN dotnet test program.tests -c Release
RUN dotnet publish program -c Release -o /app/out

# build runtime image
FROM microsoft/dotnet:2.0-runtime
WORKDIR /app
COPY --from=build-env /app/out ./
ENTRYPOINT ["dotnet", "program.dll"]
like image 161
riezebosch Avatar answered Dec 28 '22 10:12

riezebosch


There is a good blog about this. A guide to setting up a .NET Core project using Docker, with integrated unit and component tests In this blog look for component tests which is equivalent to what you are trying to do. Below dockerfile is working for me. This can be build as docker image and then can be used for running integration tests.

FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY *.sln .
COPY ["Somthing.Business/Somthing.Business.csproj", "Somthing.Business/"]
COPY ["tests/Somthing.Business.IntegrationTests/Somthing.Business.IntegrationTests.csproj", "tests/Somthing.Business.IntegrationTests/"]
COPY "Directory.*.props" . # this is centralized package manager for a project

RUN dotnet restore "tests/Somthing.Business.IntegrationTests/Somthing.Business.IntegrationTests.csproj"
COPY . .
RUN dotnet build "tests/Somthing.Business.IntegrationTests/Somthing.Business.IntegrationTests.csproj"

FROM build AS testrunner
WORKDIR /src/tests/Somthing.Business.IntegrationTests

CMD ["dotnet", "test", "--no-restore"]
like image 30
avinash k c Avatar answered Dec 28 '22 11:12

avinash k c