简体   繁体   中英

How run C# Unit tests in docker container

i Have problem with run Unit tests in docker container. I'm create ApiTests project with Nunit tests for custom class methods. Tests successfully run. Then create UnitApiTestsService project which use ApiTests.dll like:

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

Service runned by message from 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); 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:

#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. anybody might solve this trouble?

I solve this issue. Need correctly write the docker file. Before assembling the second image, I built up both projects and copy publish files from build-env publish/ folders to app/ in second image.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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