【问题标题】:Docketfile - Unit test doesn't work with multiple build targetDocketfile - 单元测试不适用于多个构建目标
【发布时间】:2023-04-07 05:58:01
【问题描述】:

我有以下 docker 文件,它有多个构建目标,包括 .net 单元测试。

由于某种原因,它在构建期间没有通过单元测试,但如果我评论 testrunner 并测试构建目标点,它就可以工作。

有什么想法吗?

我的文件夹结构:

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

结果输出:

如果我在 Docker 文件的下面一行注释,它会在构建期间成功运行单元测试

# 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

【问题讨论】:

    标签: .net docker dockerfile containers .net-core-3.1


    【解决方案1】:

    在执行docker build 时,您没有指定目标构建阶段。因此,默认情况下,它以要构建的最后阶段为目标。但是最后阶段不依赖于 testrunner 或测试阶段,因此它们都不会被构建。如果要显式运行其中一个测试阶段,可以指定 --target 选项以针对特定阶段。例如:docker build -t pokemonapi:v1 -f Dockerfile --target test .

    【讨论】:

    • 谢谢马特。
    猜你喜欢
    • 2019-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-22
    • 2019-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多