This article is intended to provide easy to use branch level code coverage tool within the Visual Studio for latest .NET Framework (.NET Core).
- 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.
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:
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
ReportGeneratorVersion variables based on your project.
-oldstyle switch fixes this issue
unit test classes are excluded from coverage report
-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 ^
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
-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:
- 2018-October-09: Initial version