November 06, 2016 by Anuraj
C# ASP.NET Core Unit Testing Code Coverage .NET Core
This post is about getting code coverage of .NET Core using Opencover. This is an update post, long back I wrote a post on code coverage using ASP.NET Core in RC days. This post is using dotnet command and 1.0 version. Opencover is a code coverage tool for .NET 2 and above, support for 32 and 64 processes with both branch and sequence points; roots proudly based in PartCover - https://github.com/OpenCover/opencover/
Here is the source file, since the post is about measuring code coverage, it is a simple math libaray with Add and Divide methods.
Once library created, you can execute dotnet build command to verify everything works fine. Once it is done, you can write the unit test file. Here is the test file.
And the project.json file for unit test project.
You require global.json file in the root directory, which used to resolve the dependencies. Execute dnu restore and dnx test command to verify your unit tests. To get the code coverage, you require Open cover, which you can install via Nuget or can download and install from here https://github.com/OpenCover/opencover/. Now you can execute the test using Open cover, which can be using following command.(Line breaks added for readability)
Here is the screenshot of opencover - running on dotnet code application.
Happy Programming :)
Copyright © 2024 Anuraj. Blog content licensed under the Creative Commons CC BY 2.5 | Unless otherwise stated or granted, code samples licensed under the MIT license. This is a personal blog. The opinions expressed here represent my own and not those of my employer. Powered by Jekyll. Hosted with ❤ by GitHub