Click here to Skip to main content
15,898,035 members
Articles / .NET / .NET Core

Unit Test: Branch Level Code Coverage for .NET Core, xUnit, OpenCover, ReportGenerator and Visual Studio Integration

Rate me:
Please Sign up or sign in to vote.
4.50/5 (2 votes)
9 Oct 2018CPOL2 min read 8.3K   2  
Get the branch level code coverage report in one click from all versions of Visual Studio

Introduction

This article is intended to provide easy to use branch level code coverage tool within the Visual Studio for latest .NET Framework (.NET Core).

Background

  • This is an updated version of the article posted for .NET framework 4 years ago.
  • I found the OpenCover with ReportGenerator is still the easy to use best open source option available to test the branch level unit test coverage for .NET. Thanks to the original developers for maintaining these good tools!
  • The article addresses the major differences in the behaviours of the latest frameworks and tools:
    • .NET Core framework changed the location of Nuget packages getting installed
    • For .NET Core, the Opencover can invoke the dotnet directly to get the coverage results instead of calling runners of the unit test framework

Summary of Steps

  1. Install Nuget packages.
  2. Enable the debug information.
  3. Build the entire solution.
  4. Execute the Coverage.bat file. The batch file performs the below operations:
    1. OpenCover captures the unit testing results and produces the coverage report in '.XML' format. Usage reference: https://github.com/OpenCover/opencover/wiki/Usage
    2. ReportGenerator produces the HTML report based upon OpenCover’s '.XML' file. Usage Reference: https://github.com/danielpalme/ReportGenerator
    3. Invoke the HTML summary report created in coverage sub-folder

Install the Dependencies via Nuget Packages

  • XUnit is inbuilt with the Visual Studio. But you need to upgrade the Nuget packages to the latest version.
  • OpenCover
  • ReportGenerator
PM> install-package OpenCover
PM> install-package ReportGenerator

Enable the Debug Information

The dotnet core binaries do not expose the required debug information expected by OpenCover by default.

We'll need to add the "DebugType" tag to each project file we want to profile:

XML
<PropertyGroup>
  <DebugType>Full</DebugType>
</PropertyGroup>

Image 1

Create Simple Batch File to be Invoked from Visual Studio

  1. Create the following .bat file in unit test project with the name Coverage.bat.
  2. In this batch file, SET OpenCoverVersion and ReportGeneratorVersion variables based on your project.
BAT
@ECHO OFF
REM @echo suppresses command line. 
REM ^ is line continuation character

REM for .NET core projects Nuget packages are installed in %UserProfile%\.nuget\packages
REM The %UserProfile% variable is a special system-wide environment variable 
REM It contains %SystemDrive%\Users\{username}

SET PackagesFolder=%UserProfile%\.nuget\packages
SET OpenCoverVersion=4.6.519
SET ReportGeneratorVersion=3.1.2

REM Some of the APIs used by OpenCover to profile the app are missing in .net Core.  
    -oldstyle switch fixes this issue
REM "-filter" to selectively include or exclude assemblies and classes from coverage results.
REM DTOs, auto generated designer files, diagrams files, 
          unit test classes are excluded from coverage report
REM Default filters are: -[mscorlib]* -[mscorlib.*]* -[System]* -[System.*]* -[Microsoft.VisualBasic]* 
@ECHO ON
%PackagesFolder%\opencover\%OpenCoverVersion%\tools\OpenCover.Console.exe ^
    -target:"C:/Program Files/dotnet/dotnet.exe" ^
    -targetargs:"test \"CommonUtilities.UnitTest.csproj\" --configuration Debug --no-build" ^
    -filter:"+[*]* -[*.Tests*]* -[*.UnitTest*]* -[*.XUnitTest*]* -[<assembly>.DataModel]* 
                                -[<assembly>.UnitTest]* -[<assembly>.Diagrams]*" ^
    -filter:-excludebyfile:*\*Designer.cs -mergebyhash ^
    -oldStyle ^
    -register:user ^
    -output:"OpenCoverReport.xml"
@ECHO OFF

REM delete old coverage files
REM /F /Q switches to delete files and directories even with readonly attribute without confirmation
DEL /F /Q .\coverage\*.*

REM Generate HTML based coverage reports
@ECHO ON
%PackagesFolder%\ReportGenerator\%ReportGeneratorVersion%\tools\reportgenerator.exe ^
 -reports:OpenCoverReport.xml -targetdir:coverage Verbosity: Error

REM invoke the html coverage summary in the browser
START "" ".\coverage\index.htm"

Visual Studio Integration

  • We can configure Coverage.bat executable as an external tool.
  • Visual Studio provides the macros to integrate the external tools with the projects. https://msdn.microsoft.com/en-us/library/c02as0cs.aspx?f=255&MSPPError=-2147217396
  • Configure the “External Tools” to Coverage.Bat in project folder as shown in the below screenshot.
  • This will execute the commands within the IDE and will display the output in Visual Studio’s “Output” window.
  • If you configured this as first External Tool, this will be treated as “External Command (N)? (by default, Visual Studio Enterprise has 3 external tools pre-configured).
  • You can add this menu item to the necessary toolbar (I configured to Build tool bar) as shown in the below steps:

Image 2

Image 3

Image 4

Image 5

Image 6

Sample Output

Image 7

History

  • 2018-October-09: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
India India
I have been programming for last 20 years on various platforms including .NET, Visual Basic 6, Oracle and SQL server.

I decided to write articles when I have free time hoping to share my thoughts with you.

To know more about me visit http://sabarinathanarthanari.com

Comments and Discussions

 
-- There are no messages in this forum --