简体   繁体   English

如何在 docker 容器中运行 C# 单元测试

[英]How run C# Unit tests in docker container

i Have problem with run Unit tests in docker container.我在 docker 容器中运行单元测试时遇到问题。 I'm create ApiTests project with Nunit tests for custom class methods.我正在为自定义类方法创建带有 Nunit 测试的 ApiTests 项目。 Tests successfully run.测试成功运行。 Then create UnitApiTestsService project which use ApiTests.dll like:然后创建使用 ApiTests.dll 的 UnitApiTestsService 项目,如:

var dockerEnvironment = Environment.GetEnvironmentVariable("DOCKER_UNIT_TEST_ENVIRONMENT")=="TRUE" ? true : false;
var info = new ProcessStartInfo
{
    FileName = "dotnet",                       
    Arguments = dockerEnvironment ? "vstest ApiTests.dll" : "vstest ../ApiTests/bin/Debug/netcoreapp2.2/ApiTests.dll",
    UseShellExecute = false,
    RedirectStandardOutput = true
};
_shedulerProcess = Process.Start(info);
var testResults = _shedulerProcess.StandardOutput.ReadToEnd();
_shedulerProcess.WaitForExit();

dockerEnvironment is true dockerEnvironment 是真的

Service runned by message from RabbitMQ.由来自 RabbitMQ 的消息运行的服务。 Locally(when i run project in me IDE) all succesfully work, but when i put UnitApiTestsService in docker image, after start this image i have same throuble on line Process.Start(info);本地(当我在我的 IDE 中运行项目时)都成功地工作,但是当我将 UnitApiTestsService 放入 docker 映像时,启动此映像后,我在 Process.Start(info); 线上遇到了同样的麻烦; with this text:用这段文字:

vstest.console process failed to connect to testhost process after 10000 seconds. This may occur due to machine slowness, please set environment variable VSTEST_CONNECTION_TIMEOUT to increase timeout. Test Run Aborted.

my Dockerfile:我的 Dockerfile:

#FROM microsoft/dotnet:2.2-sdk AS build-env
FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build-env

WORKDIR /app
COPY . .
RUN dotnet publish ./Operations/ApiTests -c Release -o publish
RUN dotnet publish ./Operations/UnitAPITestsService -c Release -o publish

WORKDIR /app/Operations/ApiTests/
RUN dotnet restore .; exit 0;
RUN dotnet build

# Build runtime image
#FROM microsoft/dotnet:2.2-sdk
FROM mcr.microsoft.com/dotnet/core/sdk:2.2
#FROM mcr.microsoft.com/dotnet/core/aspnet:2.2  

WORKDIR /app
COPY --from=build-env /app/Operations/UnitAPITestsService/publish .
ENTRYPOINT ["dotnet", "UnitAPITestsService.dll"]

RUN mkdir -p /opt/
RUN chmod -R 777 /opt/
RUN mkdir -p /opt/download
RUN chmod -R 777 /opt/download
ENV VSTEST_CONNECTION_TIMEOUT 10000
ENV DOCKER_UNIT_TEST_ENVIRONMENT TRUE

when container work, service successfully receive message from Rabbit.当容器工作时,服务成功接收到来自 Rabbit 的消息。 anybody might solve this trouble?有人可以解决这个麻烦吗?

I solve this issue.我解决了这个问题。 Need correctly write the docker file.需要正确编写docker文件。 Before assembling the second image, I built up both projects and copy publish files from build-env publish/ folders to app/ in second image.在组装第二个图像之前,我构建了两个项目并将发布文件从 build-env publish/ 文件夹复制到第二个图像中的 app/ 。

FROM microsoft/dotnet:2.2-sdk AS build-env

WORKDIR /app
COPY . .
RUN dotnet publish ./Operations/ApiTests -c Release -o publish
RUN dotnet publish ./Operations/UnitAPITestsService -c Release -o publish

WORKDIR /app/Operations/ApiTests/
RUN dotnet restore .; exit 0;
RUN dotnet build

WORKDIR /app/Operations/UnitAPITestsService/
RUN dotnet restore .; exit 0;
RUN dotnet build

FROM mcr.microsoft.com/dotnet/core/sdk:2.2

WORKDIR /app
COPY --from=build-env /app .

COPY --from=build-env /app/Operations/ApiTests/publish .
COPY --from=build-env /app/Operations/UnitAPITestsService/publish .
#WORKDIR /app/Operations/UnitAPITestsService
ENTRYPOINT ["dotnet", "UnitAPITestsService.dll"]

RUN mkdir -p /opt/
RUN chmod -R 777 /opt/
RUN mkdir -p /opt/download
RUN chmod -R 777 /opt/download
ENV VSTEST_CONNECTION_TIMEOUT 90000
ENV DOCKER_UNIT_TEST_ENVIRONMENT TRUE

Voila, everything works.瞧,一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM