12

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?

Jon Mitchell
  • 3,349
  • 5
  • 27
  • 37

3 Answers3

4
  1. Install Coverlet.MSbuild
  2. Install Coverlet.Collector
  3. Rebuild the project
  4. Click on Tools > Nuget Package Manager > Package Manager Console
  5. Run dotnet test --collect:"XPlat Code Coverage"
  6. Find the desired coverage.cobertura.xml file in the folder 'TestResults'
  7. (optional) Deploy your solution in AzureDevops if you want a graphical interface to read the file and present the results in a more convenient fashion
Luis Gouveia
  • 8,334
  • 9
  • 46
  • 68
1

Option 1

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

  • Add the following NuGet packages to your test project
    • coverlet.msbuild
    • Microsoft.NET.Test.Sdk
    • Microsoft.TestPlatform
    • Microsoft.TestPlatform.Build
  • Add a property group to your test project file (.csproj)
<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>
  • Use the MSBuild task
    • use the following command line args:
      <your-project>.csproj /p:CollectCoverage=true /t:VSTest
  • Use a Publish code coverage results task to publish the corbertura file (default name: coverage.cobertura.xml) produced by the MSBuild task
riQQ
  • 9,878
  • 7
  • 49
  • 66
  • 1
    There is a comment at the top of the linked page that says that VSTest integration only works with .NET Core applications and not .NET Framework. Can you confirm that your solution is supported for .NET Framework projects as I can't get it to work? – Jon Mitchell Apr 07 '20 at 14:35
  • @JonMitchell see my edit, i got it to work locally and produce a coverage report. The rest should work as described – riQQ Jun 16 '20 at 07:36
  • Also, https://stackoverflow.com/questions/60707310/is-it-possible-to-get-code-coverage-of-net-framework-project-using-coverlet-in confirms Option 1 to work. What error are you hitting? – riQQ Jun 16 '20 at 11:06
1

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:

  • Build the solution with MSBuild
  • Download coverlet.collector from NuGet
  • Run dotnet test with coverlet specified as the adaptor and --no-build
  • Use the ReportGenerator extension to combine the coverage reports
  • Publish the code coverage results

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'
ste-fu
  • 6,879
  • 3
  • 27
  • 46