Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is causing "Could not find data collector 'XPlat Code Coverage'" error in Docker image?

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?

like image 516
memtaus Avatar asked Jul 23 '19 17:07

memtaus


People also ask

What is XPlat code coverage?

The "XPlat Code Coverage" argument is a friendly name that corresponds to the data collectors from Coverlet. This name is required but is case insensitive. As part of the dotnet test run, a resulting coverage. cobertura. xml file is output to the TestResults directory.

What is coverlet collector?

Coverlet is a cross platform code coverage library for . NET, with support for line, branch and method coverage.


1 Answers

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

like image 173
kingkeamo Avatar answered Oct 20 '22 18:10

kingkeamo