Unit Test - Branch Level Code Coverage For .NET Core, XUnit, OpenCover, ReportGenerator and Visual Studio Integration - CodeProject
Unit Test - Branch Level Code Coverage For .NET Core, XUnit, OpenCover, ReportGenerator and Visual Studio Integration - CodeProject
rage for .NET Core, xUnit, OpenCover, ReportGenerator and Visual Studio Integ…
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
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…
We'll need to add the "DebugType" tag to each project file we want to profile:
<PropertyGroup>
<DebugType>Full</DebugType>
</PropertyGroup>
@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
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)
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.
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