3

I am testing and building my service in a docker container using Teamcity's docker build command. This works fine for the build, but I would ideally like the nunit test results to be picked up and displayed in the test tab in the same way it does if you use Teamcity's dotnet test functionality.

Is this at all possible?

Using this as a guide for creating a multi-stage build to run my tests, here is my docker file so far.

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS restore
WORKDIR /src
 
COPY ["MyService/MyService.csproj", "MyService/"]
COPY ["MyService.Tests/MyService.Tests.csproj", "MyService.Tests/"]

RUN dotnet restore "MyService/MyService.csproj"
RUN dotnet restore "MyService.Tests/MyService.Tests.csproj"

FROM restore AS build
COPY . .
RUN dotnet build "MyService/MyService.csproj" -c Release -o /app/build

FROM build AS test
RUN dotnet test "MyService.Tests/MyService.Tests.csproj" -c Release --logger "trx;LogFileName=TestResults.trx"

FROM scratch AS export-test-results
COPY --from=test /src/MyService.Tests/TestResults/*.trx .

FROM build AS publish
RUN dotnet publish "MyService/MyService.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .

ENTRYPOINT ["dotnet", "MyService.dll"]

My teamcity build step config looks like this:

dockerCommand {
    commandType = build {
        source = file {
            path = "MyService/Dockerfile"
        }
        contextDir = "."
        namesAndTags = "myservice:latest"
        commandArgs = "--network host --target export-test-results --output type=local,dest=."
    }
    param("dockerImage.platform", "linux")
}

I was sort of hoping that teamcity could pick up the .trx file if I presented it as an artefact of the step, but not sure if that's how tc works.

Has anyone managed this and if so, how?

TIA

Rich

Dutts
  • 5,781
  • 3
  • 39
  • 61

2 Answers2

1

The problem is that docker build is creating a new container, in a temporary/scratch area, that TeamCity can't see into.

I.e. your tests produce a TRX file in some folder, but that's inside the container as part of docker build. You need to get the trx file out of the container and into a place where TeamCity can pick it up.

When using docker run or docker compose the typical solution to this is to map a volume into the container. Have your tests write the trx file into that mapped folder, and when the container exits it will handily be there, ready for TeamCity to read.

But, you can't map volumes into containers during docker build

We end up needing to look for other ways to get files out. I don't have answers to that, but this other stackoverflow question does:

Docker: Copy file out of container while building it

Orion Edwards
  • 121,657
  • 64
  • 239
  • 328
0

Disclaimer: I neither tested it nor did I ever set up such a setting. But I have some experience with TeamCity, and since you didn't receive any feedback within a week, I thought it might be better than nothing

According to the TeamCity docs, I think a Build Feature called XML Report Processing could do the trick. Can you try to configure the following and see what happens?
enter image description here

mu88
  • 4,156
  • 1
  • 23
  • 47