4
votes

I'm setting up a build in DevOps Server that simply runs a docker build, runs a container, and collects test results from it. The Dockerfile uses the mcr.microsoft.com/dotnet/core/sdk:2.2 image as its base, on top of which I've installed powershell.

Running the base image and executing dotnet --version confirms it has .NET Core 2.2.301 on it. My Tests project references coverlet.collector 1.0.1 and Microsoft.NET.Test.Sdk 16.1.0. From the Coverlet documentation I've seen, that should be sufficient to generate test coverage results.

My tests run fine and generate the expected trx file when I don't use Coverlet.

Here is a simplified version of my dockerfile:

FROM myrepo/coresdk22-powershell AS build
SHELL ["pwsh", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
USER ContainerAdministrator
# Not shown: some configuration steps using powershell
WORKDIR /app
COPY webapiapp/*.csproj ./webapiapp/
WORKDIR /app/webapiapp
RUN dotnet restore --configfile ../nuget.config
WORKDIR /app/
COPY webapiapp/. ./webapiapp/
WORKDIR /app/webapiapp
RUN dotnet publish -c Release -o out

FROM build AS testrunner
WORKDIR /app/webapiapp.test
COPY webapiapp.test/. .
RUN dotnet build -c Release -o out
WORKDIR /app/webapiapp.test/out
ENV Coverage="XPlat Code Coverage"
ENTRYPOINT dotnet vstest webapiapp.test.dll --logger:trx --collect:$env:Coverage

Here is the run command:

docker run -v "c:\testresults:C:\app\webapiapp.test\out\TestResults" --rm myrepo/webapiapp:$(BUILD.BUILDID)-test

And here is the webapiapp.test.csproj:

  <PropertyGroup>
    <TargetFramework>netcoreapp2.2</TargetFramework>

    <IsPackable>false</IsPackable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="coverlet.collector" Version="1.0.1">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.1.0" />
    <PackageReference Include="xunit" Version="2.4.0" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
  </ItemGroup>

</Project>

Instead of a coverage file, I get "Could not find data collector 'XPlat Code Coverage'". What am I missing?

1
Any reason you're using dotnet vstest vs dotnet test?Tony Ranieri
dotnet test didn't work. It's been a while, so I don't remember what the error was.memtaus
When I say it didn't work, what I mean is it wouldn't even run the tests, themselves, never mind code coverage.memtaus

1 Answers

6
votes

I was getting the same error and the fix for me was to add the nuget reference below.

<PackageReference Include="coverlet.collector" Version="1.0.1" />

I was trying to get the code coverage working in my azure devops pipeline and this did the trick for me.

I was following below tutorial, with the default template api (WeatehrForecast example)

https://docs.microsoft.com/en-us/azure/devops/pipelines/ecosystems/dotnet-core?view=azure-devops