在Docker容器中运行dotnet测试 [英] Run dotnet test inside docker container
问题描述
我想在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.
DockerFile:
DockerFile:
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
推荐答案
您正在使用运行时映像来调用 sdk 命令测试。您的 final 来自 base ,而 base 来自 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屋!