Code coverage plays a more significant role in modern software development than simply being a metric; it provides insight about the code quality, the value of the tests, and the general state of your project. For .NET developers, integrating code coverage tools into the development and testing workflow provides clarity about the integrity of the application and its ability to be maintained in a production environment. Code coverage tools also facilitate agile and DevOps practices through continuous testing and continual improvements to your project.
This article introduces the importance of code coverage, describes some of the best tools in the .NET ecosystem, and describes how to apply the tool for best results in your development cycle.
What is Code Coverage
Code coverage is a metric that reveals the percentage of source code exercised by automated testing. By answering sets of questions during compilation, code coverage allows you to understand:
- Are all logical branches and decision points being tested?
- Are all code paths being executed under varying conditions?
- Is there code that is redundant, dead, or unreachable to remove?
Code coverage does not always have a practical route to reaching 100 percent coverage — nor does it always need to. Rather, a desired coverage level can put you in the position to reduce bugs, raise developer confidence, and facilitate code reviews and maintenance tasks. Consider aiming for a healthy coverage level in an effort to reduce bugs, raise developer confidence, and simplify code reviews and maintenance tasks.
Notable Code Coverage Tools for .NET Developers
There are numerous tools in the .NET ecosystem that provide reliable and flexible code coverage reporting. Each tool has plus and minus points and will be better suited to different project types and development teams.
Coverlet
Coverlet is a light weight open-source coverage tool made for .NET Core and .NET Standard projects. It works natively with xUnit, NUnit, MSTest and other popular unit testing frameworks.
Key Benefits:
- Great Integration/Coverage for .NET Core projects
- Outputs coverage data in common formats including, Cobertura, OpenCover, etc.
- Works with build automation tools including, GitHub Actions, Azure DevOps (ADO), Jenkins, etc
- Produces coverage reports well when working with ReportGenerator
Coverlet is specifically for projects that are leveraging modern .NET development technologies including microservices, containers, etc.
JetBrains dotCover
dotCover is a commercial product from JetBrains and part of the ReSharper Ultimate package. is a very capable code coverage and test analysis tool, especially with developers that leverage the .NET Framework and Microsoft Visual Studio.
Key Benefits:
- Solid Integration with Microsoft Visual Studio with ReSharper plug-in
- Integrated build support for .NET Framework, .NET Core, .NET 5/6/7 and latest builds
- Provides statement coverage and branch coverage metrics
- Good navigation, Filtering coverage results
Visual Studio Code Coverage
Visual Studio Enterprise has built-in code coverage analysis tools that integrate seamlessly with MSTest. This allows you to run unit tests and get code coverage reports without any third-party libraries!
Key Highlights:
- Built into the Visual Studio test execution workflow
- Coverage results are provided in the IDE along with source code
- Supports managed and unmanaged code
- Easy to setup and use by internal teams
Although it is only available in the Enterprise edition of Visual Studio, it is ideal for development teams already using Microsoft’s tools within the application life cycle management space.
ReportGenerator
ReportGenerator on its own is not a code coverage tool, but it does serve an important purpose in visualizing and interpreting code coverage analysis output files. It takes the outputs of raw coverage reports (e.g. from Report Generator or OpenCover) and creates rich, human-readable HTML reports. In addition, visualizations contained in the reports are rich: summaries, charts, and filters that help you and your team sift through answer questions on what code is being covered.
Key Highlights:
- Works with extensive coverage output formats
- Ability to host or share reports with teammates easily
- Ability to facilitate analysis by highlighting poor coverage files and/or lines
- Can be used effectively for local development ~ CI/CD Pipelines
Using ReportGenerator in tandem with a coverage tool like Coverlet or dotCover can enhance your test coverage metrics and make them more readable for your team.
Contemplating Why Code Coverage Should Be a Focus
Strong code coverage can provide lots of advantages beyond whether or not a test passes:
- Enhanced reliability: Applications that have better coverage invariably will be more reliable in production because there are many more execution paths that have been tested.
- Better maintainability: When developers have high coverage, they can refactor with confidence, knowing that the tests will detect any regressions.
- Faster feedback cycles: Integrated into CI/CD tools, coverage tools will allow developers to identify defects much earlier in the development cycle.
- Greater confidence: Teams can make better decisions regarding going – it will be based upon coverage thresholds the trends, etc. related to technical debt and the need for refactoring.
While code coverage shouldn’t be the only metric used to determine code quality, it provides invaluable information when used with other practices such as Static Analysis, Peer Reviews, and Integration Testing.
Integrating Code Coverage into CI/CD Pipelines
There has been a shift in modern practices related to DevOps and automation surrounding continuous delivery and feedback. Code coverage can be integrated into CI/CD pipelines by using popular tools, particularly those in the GitHub ecosystem (GitHub Actions, GitHub, Azure DevOps, GitLab CI listings, etc.). Typically, the CI/CD workflow encompasses:
Executing a primary test suite that has coverage enabled
Generating coverage reports in compatible formats usable by reporting tools with some platforms supporting multiple file formats.
Copying or uploading test results to tools like Codecov, SonarCloud, or into internal dashboards and repositories.
Setting code coverage thresholds that can fail a build or warn of a fail when the threshold coverage is reached or lowered.
Code Quality and Testing from EmpiricalEdge.com
At EmpiricalEdge.com, we focus on helping businesses build and maintain high quality .NET applications. Our services will allow you to get the most out of your development process, and enhance your engineering capabilities.
Our .NET services include:
- Creating and implementing solid unit and integration testing plans
- Conducting code quality audits and refactoring strategies
- Creating CI/CD pipelines with integrated code coverage and static analysis
- Performance and maintainability improvements for .NET projects
Whether you are creating a new application, updating your legacy systems, or building a better development team, EmpiricalEdge.com will give you expert advice and technical delivery to ensure your software is reliable, testable, and ready for production.
Contact us today to learn how our .NET experience can help you build better code, faster.