【问题标题】:Run dotnet test inside docker container在 docker 容器中运行 dotnet test
【发布时间】:2018-05-01 20:36:57
【问题描述】:

我想在 docker 容器中运行 dotnet test 命令,但我无法弄清楚该命令的放置位置。该项目是一个 .NET Core 2.1 测试项目。这样做的原因是我想运行端到端集成测试,这需要我的所有容器都在运行。

Docker 文件:

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

【问题讨论】:

    标签: docker .net-core docker-compose


    【解决方案1】:

    您正在使用 runtime 映像来调用 sdk 命令测试。您的 final 来自 basebase 来自 runtime

    我在构建容器时成功地将单元测试用作中间步骤,但从未使用 docker-compose 进行集成测试。主要区别在于我使用了RUN 命令而不是entrypoint/cmd,因此在构建容器时测试已经在执行。主要优点是测试失败时没有 final 图像。但话又说回来,这是纯粹的单元测试,没有集成测试。虽然我可以想象这也会起作用。

    这是我的完整示例:

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

    【讨论】:

      【解决方案2】:

      您没有向我们提供错误日志或其他内容,但我猜您正在尝试在未安装 dotnet sdk 的图像上运行 dotnet test Sombra.IntegrationTests.csproj(仅安装 dotnet runtime) .

      dotnet runtime 是 dotnet core 的一个版本,不能执行命令(如 testbuild)。它只能执行 dll(因此得名“runtime”)。

      【讨论】:

      • 我没有错误日志,因为容器刚刚以代码 0 退出。SDK 已安装,如您在提供的 DockerFile 中看到的那样
      • 它没有安装在base 容器中。您可以尝试从 bash 运行它吗? docker run -it container bash 然后从那里自己运行它。
      【解决方案3】:

      关于这个有一个很好的博客。 A guide to setting up a .NET Core project using Docker, with integrated unit and component tests 在此博客中查找与您正在尝试做的等效的组件测试。 下面的 dockerfile 对我有用。这可以构建为 docker 镜像,然后可以用于运行集成测试。

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

      【讨论】:

      • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
      猜你喜欢
      • 1970-01-01
      • 2019-08-28
      • 1970-01-01
      • 1970-01-01
      • 2022-10-04
      • 1970-01-01
      • 2020-11-27
      • 2015-03-05
      • 1970-01-01
      相关资源
      最近更新 更多