question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Data collection: Could not find data collector 'XPlat Code Coverage"

See original GitHub issue

Hello,

I can’t show code due to it being for the company I work for but basically I am getting this error:

Data collection : Could not find data collector ‘XPlat Code Coverage’

I saw this error on another post but I don’t think my cause is the same.

What I’m doing to get this message:

dotnet vstest Tests.dll --collect:"XPlat Code Coverage" or dotnet vstest Tests.dll --collect:"XPlat Code Coverage" --settings coverletArgs.runsettings

I tried using the runsettings file as well but that didn’t change anything.

Output of dotnet --info:

.NET Core SDK (reflecting any global.json):
  Version:    2.2.401
  Commit:   7299b316c13

Runtime Environment:
  OS Name: centos
  OS Version: 7
  OS Platform: Linux
  RID: centos.7-x64
  Base Path:  /usr/share/dotnet/sdk/2.2.401/

Host (useful for support):
  Version: 2.2.6
  Commit: 7dac9b1b51

.NET Core SDKs installed:
  2.2.401 [/usr/share/dotnet/sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.All 2.1.12 [/usr/share/dotnet/shared/Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.2.6 [/usr/share/dotnet/shared/Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.12 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.2.6 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.0.7 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.12 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.6 /usr/share/dotnet/shared/Microsoft.NETCore.App]

I have the following package references as well in my test projects:

<PackageReference Include="coverlet.collector" Version="1.0.1">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.1.0" />
<PackageReference Include="MSTest.TestAdapter" Version="1.3.2" />
<PackageRefernece Include="MSTest.TestFramework" Version="1.3.2" />

and <TargetFramework>netcoreapp2.2</TargetFramework>

Am I missing something or doing something wrong?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:14 (1 by maintainers)

github_iconTop GitHub Comments

25reactions
granadacodercommented, Feb 27, 2020

I’ll do a mini followup, since I just tackled this (with your suggestions! @marco)

For future readers.

I was getting the error:

Data collection: Could not find data collector 'XPlat Code Coverage"

(everything below is in the context of using a LINUX build machine/image, not windows…there is a difference !!)

So when you do a regular dotnet build

you’ll get an output like this:

\src\UnitTests\bin\Release\netcoreapp3.1\win-x64\MyProjectName.UnitTests.dll

At this point, if you were to look in the directory:

\src\UnitTests\bin\Release\netcoreapp3.1\win-x64\

you’ll find NO “coverlet” dlls/files.

examples:

coverlet.core.dll coverlet.collector.dll

What marco is saying here is “Yep, that’s the issue, you don’t have the magic dlls”. And to over come that…you “dotnet publish” your UnitTest project. ((<< this is a little bit contrary to what you usually do, which is publish your real-application layer.) (<<this is what was tripping me up initially).

So now if you “dotnet publish” your UNIT TEST (.csproj)…

You will get something like this: (emphasis on the last directory in the chain of “publish”)

\src\UnitTests\bin\Release\netcoreapp3.1\win-x64\publish\MyProjectName.UnitTests.dll Now to look in the directory

\src\UnitTests\bin\Release\netcoreapp2.1\win-x64\publish\ You should now see files like this:

\src\UnitTests\bin\Release\netcoreapp3.1\win-x64\publish\coverlet.core.dll
\src\UnitTests\bin\Release\netcoreapp3.1\win-x64\publish\coverlet.collector.dll

Don’t focus on the exact complete paths, focus that the “publish” directory has the necessary coverlet files.

NOW, you run

dotnet test --collect:"XPlat Code Coverage" --logger:trx --ResultsDirectory:/MyTestResults "\src\UnitTests\bin\Release\netcoreapp3.1\win-x64\publish\MyProjectName.UnitTests.dll" and NOW IT WORKS !!

Remember, “coverlet.core.dll” and “coverlet.collector.dll” won’t show up by magic. You need to add nuget references to your UnitTests.csproj (<<whatever your csproj name is)

I’ll post my current (working) example below. Don’t get too caught up in the versions (as long as they are not super old)…

  <ItemGroup>
    <PackageReference Include="FluentAssertions" Version="5.10.0" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
    <PackageReference Include="Moq" Version="4.13.1" />
    <PackageReference Include="MSTest.TestAdapter" Version="2.0.0" />
    <PackageReference Include="MSTest.TestFramework" Version="2.0.0" />
    <PackageReference Include="coverlet.collector" Version="1.2.0" />>
  </ItemGroup>

The FluentAssertions and Moq are not important to “coverlet” code coverage…but they were in my file, so I posted them.

The magic takeaway is

  1. Have a PackageReference to “coverlet.collector” in your unit test csproj.
  2. You can’t get coverage from the results of “dotnet build”, you need “dotnet publish” (to get the magic coverlet files in the output directory. “dotnet publish” of a unit test project is a little counter intuitive, but it is what you need here.
  3. run your “dotnet test” on the myunittests.dll that is in the publish directory.

Thanks Marco ! Seeing coverage on the linux build image was awesome!

Final documentation note:

My “coverage” report came out at:

\MyTestResults\abcdabcd-abcd-abcd-abcd-abcdabcdabcd\coverage.cobertura.xml

where “abcdabcd-abcd-abcd-abcd-abcdabcdabcd” is SOME RANDOM GUID.

(the random guid subfolder is a different subject altogether, and is related to “dotnet test”, NOT specifically this coverage collector…and is outside the scope)

Final helpful breadcrumb (windows vs linux)

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

scroll down to “If you’re building on Linux or macOS,”

This will give you some extra clues that “dotnet build”, “dotnet test” and/or “dotnet publish” are not the exact same on linux.

3reactions
vagisha-nidhicommented, Aug 19, 2019

In case of dotnet vstest, the coverlet.collector will be picked from the output directory(where the test dll resides). So in general, for dynamic code coverage also, we follow the pattern that first we do a dotnet publish which puts code coverage datacollector and the test dlls in the publish directory and then we run the tests from there. The XPlat Code Coverage should run the same way. Please try this. Run dotnet publish and check if the collector is present in the publish directory. Then run the tests from the test dll present in this directory. Use the latest sdk. Do let me know if this doesn’t work.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What is causing "Could not find data collector 'XPlat Code ...
I'm setting up a build in DevOps Server that simply runs a docker build, runs a container, and collects test results from it....
Read more >
.NET CORE VStest coverage - Developer Community
Data collection : Could not find data collector 'Code Coverage'. I add also a step Publish code coverage from D:\a\_temp\*.trx with JaCoco ...
Read more >
Use code coverage for unit testing - .NET
There are two types of code coverage tools: DataCollectors: DataCollectors monitor test execution and collect information about test runs.
Read more >
NET Core coverage with coverlet - hectormartinez.dev
Collect the coverage. With our function and test created, we can start to get that coverage. We should pass the flag --collect="XPlat Code...
Read more >
coverlet
Coverlet is a cross platform code coverage framework for .NET, with support for line, branch and method coverage. It works with .NET Framework...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found