Sunteți pe pagina 1din 8

09/10/2018 Unit Test: Branch Level Code Coverage for .

rage for .NET Core, xUnit, OpenCover, ReportGenerator and Visual Studio Integ…

Unit Test: Branch Level Code Coverage for .NET


Core, xUnit, OpenCover, ReportGenerator and Visual
Studio Integration
Sabarinathan A, 9 Oct 2018

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

https://www.codeproject.com/Articles/1262790/Unit-Test-Branch-Level-Code-Coverage-for-NET-Core?display=Print 1/8
09/10/2018 Unit Test: Branch Level Code Coverage for .NET Core, xUnit, OpenCover, ReportGenerator and Visual Studio Integ…

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:

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

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.

@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\*.*
https://www.codeproject.com/Articles/1262790/Unit-Test-Branch-Level-Code-Coverage-for-NET-Core?display=Print 2/8
09/10/2018 Unit Test: Branch Level Code Coverage for .NET Core, xUnit, OpenCover, ReportGenerator and Visual Studio Integ…

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:

https://www.codeproject.com/Articles/1262790/Unit-Test-Branch-Level-Code-Coverage-for-NET-Core?display=Print 3/8
09/10/2018 Unit Test: Branch Level Code Coverage for .NET Core, xUnit, OpenCover, ReportGenerator and Visual Studio Integ…

https://www.codeproject.com/Articles/1262790/Unit-Test-Branch-Level-Code-Coverage-for-NET-Core?display=Print 4/8
09/10/2018 Unit Test: Branch Level Code Coverage for .NET Core, xUnit, OpenCover, ReportGenerator and Visual Studio Integ…

https://www.codeproject.com/Articles/1262790/Unit-Test-Branch-Level-Code-Coverage-for-NET-Core?display=Print 5/8
09/10/2018 Unit Test: Branch Level Code Coverage for .NET Core, xUnit, OpenCover, ReportGenerator and Visual Studio Integ…

https://www.codeproject.com/Articles/1262790/Unit-Test-Branch-Level-Code-Coverage-for-NET-Core?display=Print 6/8
09/10/2018 Unit Test: Branch Level Code Coverage for .NET Core, xUnit, OpenCover, ReportGenerator and Visual Studio Integ…

Sample Output

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)

About the Author


Sabarinathan A
Architect
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


https://www.codeproject.com/Articles/1262790/Unit-Test-Branch-Level-Code-Coverage-for-NET-Core?display=Print 7/8
09/10/2018 Unit Test: Branch Level Code Coverage for .NET Core, xUnit, OpenCover, ReportGenerator and Visual Studio Integ…

Comments and Discussions


0 messages have been posted for this article Visit https://www.codeproject.com/Articles/1262790/Unit-Test-Branch-Level-
Code-Coverage-for-NET-Core to post and view comments on this article, or click here to get a print view with messages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile Article Copyright 2018 by Sabarinathan A
Web01-2016 | 2.8.180920.1 | Last Updated 9 Oct 2018 Everything else Copyright © CodeProject, 1999-2018

https://www.codeproject.com/Articles/1262790/Unit-Test-Branch-Level-Code-Coverage-for-NET-Core?display=Print 8/8

S-ar putea să vă placă și