Jon Mitchell
Jon Mitchell

Reputation: 3429

Can you use Coverlet to get code coverage data in a .NET Framework project?

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

Answers (4)

GaTechThomas
GaTechThomas

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):

  1. In VS Solution Explorer, right click on the project to convert and select Upgrade
  2. Select Upgrade project features

Command line options are also available. IIRC, it's a dotnet cli migration tool that can be added in.

Upvotes: 0

Luis Gouveia
Luis Gouveia

Reputation: 8945

  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

Upvotes: 5

ste-fu
ste-fu

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:

  • 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'

Upvotes: 2

riQQ
riQQ

Reputation: 12733

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

Upvotes: 1

Related Questions