简体   繁体   中英

Docketfile - Unit test doesn't work with multiple build target

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?

My Folder structure: 在此处输入图像描述

#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"]

Result output: 在此处输入图像描述

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM