Reputation: 3429
I have a large .NET Framework solution and want to start collecting code coverage data as part of our build pipeline (as well as on our local developer machines).
On the Coverlet GitHub page it says that it supports .NET Framework projects but all the examples are using the dotnet test
CLI command.
Is it possible to use Coverlet for this or should I be looking at something like OpenCover?
Upvotes: 13
Views: 12328
Reputation: 6085
I had so many issues trying to get coverlet installed, with the need being to generate a cobertura.xml file (vstest collector "XPlat Code Coverage"). Eventually I found discussion around the test project needing to be SDK style csproj file.
Converting the csproj to SDK style solved the problem for me!
If you're using Visual Studio, you can automatically convert most project types automatically (webapi projects being an exception). To convert a project to SDK style using Visual Studio (VS):
Upgrade
Upgrade project features
Command line options are also available. IIRC, it's a dotnet
cli migration tool that can be added in.
Upvotes: 0
Reputation: 8945
dotnet test --collect:"XPlat Code Coverage"
Upvotes: 5
Reputation: 7464
I found that all of these methods has issues, building a large repo with XAML applications, and some old project format *.*proj files. The solution that worked for me was:
One issue in particular was that setting enableCodeCoverage: true
use the MS CoverCoverage.exe which then prevents viewing the nicely formatted results in Azure DevOps
In yaml it looks like:
- task: NuGetCommand@2
displayName: Restore NuGet Packages
inputs:
command: 'restore'
restoreSolution: Path/To/My.sln
feedsToUse: 'select'
vstsFeed: 'MyCompany/PrivateFeed'
includeNuGetOrg: true
- task: MSBuild@1
displayName: 'Build'
inputs:
solution: Path/To/My.sln
msbuildArchitecture: 'x64'
configuration: Release
msbuildArguments: /p:DebugSymbols=true /p:DebugType=portable -m
- task: NuGetCommand@2
displayName: 'Restore Coverlet Adapter'
inputs:
command: custom
restoreDirectory: .\
arguments: 'install coverlet.collector -Version 3.0.3 -ExcludeVersion'
- task: DotNetCoreCLI@2
inputs:
command: test
arguments: Path/To/My.sln --no-build -a $(Build.SourcesDirectory)\coverlet.collector\build\netstandard1.0 --collect:"XPlat Code Coverage"
- task: reportgenerator@4
inputs:
reports: $(Pipeline.Workspace)\**\coverage.cobertura.xml
targetdir: 'coveragereport'
reporttypes: 'HtmlInline_AzurePipelines;Cobertura'
- task: PublishCodeCoverageResults@1
inputs:
codeCoverageTool: 'Cobertura'
summaryFileLocation: 'coveragereport/cobertura.xml'
reportDirectory: 'CoverageReport'
Upvotes: 2
Reputation: 12733
Option 1
Use the task Visual Studio Test
Create a .runsettings file and configure Coverlet in the .runsettings (see https://github.com/tonerdo/coverlet/blob/master/Documentation/VSTestIntegration.md#coverlet-options-supported-by-vstest-integration)
Reference the .runsettings file in the task
Tick the option Code coverage enabled
If this doesn't work, use a Publish code coverage results
task, to publish the corbertura file (default name: coverage.cobertura.xml
) produced by the test task
Option 2
<PropertyGroup>
<VSTestTaskAssemblyFile>$(MSBuildThisFileDirectory)\..\packages\Microsoft.TestPlatform.Build.16.6.1\lib\netstandard2.0\Microsoft.TestPlatform.Build.dll</VSTestTaskAssemblyFile>
<VSTestConsolePath>$(MSBuildThisFileDirectory)..\packages\Microsoft.TestPlatform.Portable.16.6.1\tools\netcoreapp2.1\vstest.console.dll</VSTestConsolePath>
<CoverletOutputFormat>cobertura</CoverletOutputFormat>
</PropertyGroup>
MSBuild
task
<your-project>.csproj /p:CollectCoverage=true /t:VSTest
Publish code coverage results
task to publish the corbertura file (default name: coverage.cobertura.xml
) produced by the MSBuild
taskUpvotes: 1