在Docker容器中运行dotnet测试

编程入门 行业动态 更新时间:2024-10-25 09:39:21
本文介绍了在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.

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测试

本文发布于:2023-11-15 23:16:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1598832.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:容器   测试   Docker   dotnet

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!