I have below docker file, which has multiple build targets including .net unit test.
For some reason it doesn't hit the unit test during the build, but if I comment testrunner and test build target points it works.
Any idea?
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM mcr.microsoft.com/dotnet/core/runtime:3.1 AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
WORKDIR /src
COPY *.sln .
COPY ["Pokemon.API/*.csproj", "./Pokemon.API/"]
COPY ["Pokemon.Core/*.csproj", "./Pokemon.Core/"]
COPY ["Pokemon.Models/*.csproj", "./Pokemon.Models/"]
COPY ["Pokemon.Test/*.csproj", "./Pokemon.Test/"]
RUN dotnet restore
# copy full solution over
COPY . .
RUN dotnet build
# create a new build target called testrunner
FROM build AS testrunner
WORKDIR /src/Pokemon.Test
CMD ["dotnet", "test", "--logger:trx"]
# run the unit tests
FROM build AS test
WORKDIR /src/Pokemon.Test
RUN dotnet test --logger:trx
FROM build AS release
WORKDIR "/src/Pokemon.API"
RUN dotnet build "Pokemon.API.csproj" -c Release -o /app/build
FROM build AS publish
WORKDIR "/src/Pokemon.API"
RUN dotnet publish "Pokemon.API.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Pokemon.API.dll"]
If I comment below line in the Docker file, it successfully run the unit test during the build
# create a new build target called testrunner
#FROM build AS testrunner
#WORKDIR /src/Pokemon.Test
#CMD ["dotnet", "test", "--logger:trx"]
# run the unit tests
#FROM build AS test
#WORKDIR /src/Pokemon.Test
RUN dotnet test --logger:trx
You're not specifying a target build stage when executing docker build
. So, by default, it targets the final stage to be built. But the final stage doesn't have a dependency on the testrunner or test stages so neither of them will get built. If you want to explicitly run one of the test stages, you can specify the --target
option to target a specific stage. For example: docker build -t pokemonapi:v1 -f Dockerfile --target test .