I am trying to run code coverage using coverlet. My test projects are SDK style and have coverlet.collector
installed, although some of the referenced projects are not yet in the SDK style. The projects are .NET 4.8.
Fist question is about the SDK style projects. I can see the docs saying that coverlet only supports SDK style projects, but is this for the test projects, or does it include the project referenced in the test project as well?
I build the projects, and then run dotnet test
against the dll files:
dotnet test C:\git\Repositories\xxxx\bin\AnyCPU\Release\Test\aaaaaaaa.dll --collect:"XPlat Code Coverage" --settings:CodeCoverage.runsettings --results-directory C:\git\Repositories\xxxx\TestResults --logger 'trx;LogFilePrefix=aaaaaaaaa.dll' -- RunConfiguration.TargetPlatform=x64 MSTest.DeploymentEnabled=false
But when running the tests I get the following error:
VERBOSE: Data collection : Unable to find a datacollector with friendly name 'XPlat Code Coverage'.
VERBOSE: Data collection : Could not find data collector 'XPlat Code Coverage'
I googled around about this error and all the answers point to not having coverlet.collector
referenced in the test project, which is not the case.
I have used coverlet in the past, but against csproj
files and .NET 5+ and never had problems. But this project's pipeline is running the tests against the dll, it is something I cannot change and the project cannot be migrate to newer .NET versions either.
Is there any coverlet limitations in a scenario like this?
Below is an example of one of my test projects:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net48</TargetFramework>
<IsPackable>false</IsPackable>
<RootNamespace>xxxxxxx</RootNamespace>
<OutputPath>..\..\bin\$(Platform)\$(Configuration)\Test\Shared</OutputPath>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.0.4" />
<PackageReference Include="MSTest.TestFramework" Version="3.0.4" />
<PackageReference Include="ApprovalTests" Version="5.7.3" />
<PackageReference Include="ApprovalUtilities" Version="5.7.3" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.5" />
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="xxxxxx.csproj" />
<ProjectReference Include="yyyyyy.csproj" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\..\Properties\SharedAssemblyInfo.cs">
<Link>Properties\SharedAssemblyInfo.cs</Link>
</Compile>
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
</ItemGroup>
<Target Name="CopyApprovalFiles" AfterTargets="PostBuildEvent">
<ItemGroup>
<ApprovedFiles Include="**\*.approved.txt" />
</ItemGroup>
<Copy SourceFiles="@(ApprovedFiles)" DestinationFolder="$(OutputPath)\ApprovedFiles" />
</Target>
</Project>
And my CodeCoverage.runsettings
:
<?xml version="1.0" encoding="utf-8" ?>
<RunSettings>
<DataCollectionRunSettings>
<DataCollectors>
<DataCollector friendlyName="XPlat Code Coverage">
<Configuration>
<ExcludeByFile>**/*Test*/**/*.*</ExcludeByFile>
</Configuration>
</DataCollector>
</DataCollectors>
</DataCollectionRunSettings>
</RunSettings>