coverlet: Data collection: Could not find data collector 'XPlat Code Coverage"
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?
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 15 (1 by maintainers)
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.dllAt 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.dllNow to look in the directory\src\UnitTests\bin\Release\netcoreapp2.1\win-x64\publish\You should now see files like this: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)…
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
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.xmlwhere “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.
This should really be part of the documentation. After many wasted hours I arrived to the same solution, and I wish I found this thread sooner.
An additional tweak to this is to use
GeneratePathPropertyto avoid handcoding the name and the version of the package:Though it’s worth noting, that the property may not be available immediately, and can only be accessed from a target, e.g.:
I wanted to share alternative to dotnet publish solution, which in some situations could be preferable, you can provide path to data collector as follows:
dotnet test "Build\**\*Tests.dll" --collect "XPlat Code Coverage" /testadapterpath:NUGET_PACKAGE_PATH\coverlet.collector\3.0.3\build\netstandard1.0In 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 adotnet publishwhich 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. Rundotnet publishand 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.@vagisha-nidhi confirm that works. @itsryankwon can you try to publish before?Something like:
I was missing the coverlet package reference in my .csproj in its entirety. Thanks @RussKie
@RussKie thanks for this info this should unblock me!