Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 457
Jon Mitchell Avatar asked Mar 12 '20 10:03

Jon Mitchell


People also ask

Which plugin is used for getting code coverage for .NET projects?

Cobertura Plugin for the code coverage data.

What is coverlet code coverage?

Coverlet is an open source project on GitHub that provides a cross-platform code coverage framework for C#. Coverlet is part of the . NET foundation. Coverlet collects Cobertura coverage test run data, which is used for report generation.

How do you view code coverage with coverlet and Visual Studio 2019?

The first step is to head to the Extensions menu and select Manage Extensions. Then, search Run Coverlet Report and install it - you have to close all Visual Studio instances to install it.

How do I get code coverage in Visual Studio?

On the Test menu, select Analyze Code Coverage for All Tests. You can also run code coverage from the Test Explorer tool window. Show Code Coverage Coloring in the Code Coverage Results window. By default, code that is covered by tests is highlighted in light blue.


Video Answer


2 Answers

  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
like image 194
Luis Gouveia Avatar answered Sep 28 '22 07:09

Luis Gouveia


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

  • 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
like image 33
riQQ Avatar answered Sep 28 '22 06:09

riQQ