在 docker 容器内运行 dotnet 测试 [英] Run dotnet test inside docker container

查看:86
本文介绍了在 docker 容器内运行 dotnet 测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 docker 容器中运行 dotnet test 命令,但我就是不知道把命令放在哪里.该项目是一个 .NET Core 2.1 测试项目.这样做的原因是我想运行端到端集成测试,这要求我的所有容器都在运行.

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.

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

docker-compose.yml

version: '3'

services:
  sombra.integrationtests:
    image: sombra.integrationtests
    build:
      context: .
      dockerfile: Sombra.IntegrationTests/Dockerfile
    depends_on:
      - rabbitmq

推荐答案

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

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

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

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.

这是我的完整示例:

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

这篇关于在 docker 容器内运行 dotnet 测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆