我在 docker 容器中运行单元测试时遇到问题。我正在为自定义类方法创建带有 Nunit 测试的 ApiTests 项目。测试成功运行。然后创建使用 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 为真
服务由来自 RabbitMQ 的消息运行。在本地(当我在我的 IDE 中运行项目时)都成功工作,但是当我将 UnitApiTestsService 放在 docker 映像中时,启动此映像后,我在 Process.Start(info); 与此文本:
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.
我的 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
当容器工作时,服务成功接收到来自 Rabbit 的消息。有人可以解决这个问题吗?