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
- Install Nuget packages.
- Enable the debug information.
- Build the entire solution.
- Execute the Coverage.bat file. The batch file performs the below operations:
- OpenCover captures the unit testing results and produces the coverage report in '.XML' format. Usage reference: https://github.com/OpenCover/opencover/wiki/Usage
- ReportGenerator produces the HTML report based upon OpenCover’s '.XML' file. Usage Reference: https://github.com/danielpalme/ReportGenerator
- 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:
<PropertyGroup>
<DebugType>Full</DebugType>
</PropertyGroup>
Create Simple Batch File to be Invoked from Visual Studio
- Create the following .bat file in unit test project with the name Coverage.bat.
- 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\*.*
REM Generate HTML based coverage reports
@ECHO ON
%PackagesFolder%\ReportGenerator\%ReportGeneratorVersion%\tools\reportgenerator.exe ^
-reports:OpenCoverReport.xml -targetdir:coverage Verbosity: Error
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:
Sample Output
History
- 2018-October-09: Initial version