Docker教程 · 2024年11月11日

在容器中运行 .NET 测试

在容器中运行 .NET 测试

开始,完成本指南前面的所有部分 。

测试是现代软件开发的重要组成部分。测试对于不同的开发团队来说意味着很多事情。有单元测试、集成测试和端到端测试。在本指南中,您将了解在开发和构建时在 Docker 中运行单元测试。

示例应用程序的tests目录中已经有一个 xUnit 测试。在本地开发时,您可以使用 Compose 来运行测试。

在目录中运行以下命令docker-dotnet-sample以在容器内运行测试。


$ docker compose run --build --rm server dotnet test /source/tests 

您应该看到包含以下内容的输出。


Starting test execution, please wait... A total of 1 test files matched the specified pattern.  Passed! - Failed: 0, Passed: 1, Skipped: 0, Total: 1, Duration: < 1 ms - /source/tests/bin/Debug/net6.0/tests.dll (net6.0) 

要了解有关该命令的更多信息,请参阅 。

要在构建时运行测试,您需要更新 Dockerfile。您可以创建一个运行测试的新测试阶段,或在现有构建阶段运行测试。对于本指南,更新 Dockerfile 以在构建阶段运行测试。

以下是更新后的 Dockerfile。


# syntax=docker/dockerfile:1  FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:6.0-alpine AS build ARG TARGETARCH COPY . /source WORKDIR /source/src RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages \  dotnet publish -a ${TARGETARCH/amd64/x64} --use-current-runtime --self-contained false -o /app RUN dotnet test /source/tests  FROM mcr.microsoft.com/dotnet/sdk:6.0-alpine AS development COPY . /source WORKDIR /source/src CMD dotnet run --no-launch-profile  FROM mcr.microsoft.com/dotnet/aspnet:6.0-alpine AS final WORKDIR /app COPY --from=build /app . ARG UID=10001 RUN adduser \  --disabled-password \  --gecos "" \  --home "/nonexistent" \  --shell "/sbin/nologin" \  --no-create-home \  --uid "${UID}" \  appuser USER appuser ENTRYPOINT ["dotnet", "myWebApp.dll"]

运行以下命令以构建阶段为目标构建镜像并查看测试结果。包括--progress=plain查看构建输出、--no-cache确保测试始终运行以及--target build针对构建阶段。


$ docker build -t dotnet-docker-image-test --progress=plain --no-cache --target build . 

您应该看到包含以下内容的输出。


#11 [build 5/5] RUN dotnet test /source/tests #11 1.564 Determining projects to restore... #11 3.421 Restored /source/src/myWebApp.csproj (in 1.02 sec). #11 19.42 Restored /source/tests/tests.csproj (in 17.05 sec). #11 27.91 myWebApp -> /source/src/bin/Debug/net6.0/myWebApp.dll #11 28.47 tests -> /source/tests/bin/Debug/net6.0/tests.dll #11 28.49 Test run for /source/tests/bin/Debug/net6.0/tests.dll (.NETCoreApp,Version=v6.0) #11 28.67 Microsoft (R) Test Execution Command Line Tool Version 17.3.3 (x64) #11 28.67 Copyright (c) Microsoft Corporation. All rights reserved. #11 28.68 #11 28.97 Starting test execution, please wait... #11 29.03 A total of 1 test files matched the specified pattern. #11 32.07 #11 32.08 Passed! - Failed: 0, Passed: 1, Skipped: 0, Total: 1, Duration: < 1 ms - /source/tests/bin/Debug/net6.0/tests.dll (net6.0) #11 DONE 32.2s 

要了解有关构建和运行测试的更多信息,请参阅 。

在本部分中,您了解了如何在使用 Compose 进行本地开发时运行测试以及如何在构建映像时运行测试。

相关信息:

接下来,您将学习如何使用 GitHub Actions 设置 CI/CD 管道。