February 12, 2016 by Anuraj
ASP.NET5 MVC ASP.NET Core Code Coverage Unit Testing
This post is about getting code coverage of ASP.NET Core using Opencover. In computer science, code coverage is a measure used to describe the degree to which the source code of a program is tested by a particular test suite. A program with high code coverage has been more thoroughly tested and has a lower chance of containing software bugs than a program with low code coverage. 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.
And here is the project.json file for the library.
Once library created, you can execute dnu 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)
Make sure you’re using proper dnx executable (You can get the exact name and location, in C:\Users[USERNAME].dnx\runtimes), the lib location should be point to the dnu build output location. Once it completed successfully, you will see some output in the console window.
It generates coverage.xml file, which you can be used to generate the output report using report generator tool, which can be downloaded from http://danielpalme.github.io/ReportGenerator/
To generate report, you need to execute,
Once report generated, open the index.htm file, which is available in the report directory.
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