Code coverage is one of the most common metrics that helps software developers and quality assurance (QA) specialists assess testing efficiency. Evaluating code coverage helps you determine testing completeness, identify unnecessary code, and detect bugs.
In this article, we briefly explore what code coverage is, list the most popular tools for measuring it, and show how to evaluate the percentage of code coverage using the open-source OpenCover tool. This article will be helpful for developers, testers, and QA specialists who want to learn how to use the OpenCover tool for code coverage measurement.
Back in December 2017, I wrote about starting to use NDepend for static analysis. It’s a fantastic to. Tagged with coverage, ndepend, opencover.
Contents:
Code coverage is a measure of the percentage of program source code covered with unit tests. It can vary from 0% to 100%.
While test coverage measures the number of performed tests and monitors the quality of testing, code coverage indicates the percentage of lines of code covered with unit tests.
Why do you need to measure code coverage?
Evaluating code coverage is an essential part of test-driven development, which is based on writing tests before writing code. The idea is to reduce the amount of unnecessary code and avoid bugs.
Read also:
Test-Driven Development vs Behavior-Driven Development: What is the Difference?
One of the crucial benefits of analyzing code coverage is that it gives developers an understanding of whether there are enough tests in the unit test suite or whether they should create more tests to cover as many lines of code as possible.
Let’s take a look at the opportunities code coverage evaluation provides to developers:
Despite its advantages, measuring code coverage isn’t a silver bullet for checking code efficiency. One of the biggest challenges related to code coverage is interpreting the results.
Complete code coverage doesn’t mean that a project is fully tested. Even an empty method can be 100% covered, but this code is useless. 80% coverage is also a decent result, but there’s a risk that this 80% won’t cover the main scenario.
It’s essential to remember that code coverage results can’t point to flaws in the logic of your code. They also won’t report on the absence of features that should have been implemented.
Another crucial thing is to understand when you should concentrate on a high coverage percentage and when you shouldn’t.
It’s easier to cover more code with unit tests in small and mid-sized projects — and when using test automation. And it’s almost impossible to achieve high code coverage in a large-scale project with complicated logic that can lead to multiple errors. Google has spent more than a decade attempting to refine their coverage infrastructure and validate the most suitable approach.
Covering code with tests can be time-consuming and, therefore, costly. It’s reasonable to do so when you’re already familiar with the entire codebase, have information about recent and planned changes, and know specific requirements. However, if code is old and has gone through multiple changes — and if you don’t have clear requirements on how it should perform — covering it with tests will take lots of time and most likely won’t be very helpful.
Related services
Specialized Quality Assurance & Testing SolutionsCovering code with unit tests is as time-consuming as creating code. That’s why there’s a wide range of tools, both paid and free, that can accelerate the evaluation of code coverage.
Most tools for measuring code coverage are commercial, like NCover, dotCover, NCrunch, and NDepend. Also, Microsoft Visual Studio is equipped with a convenient code coverage tool with an attractive and intuitive user interface. However, it’s only available in the paid Visual Studio Enterprise version.
On the bright side, there are a fair number of efficient open-source code coverage tools. The most popular are:
In our projects, we mostly use OpenCover, which has proved its efficiency over the years and has numerous benefits:
Now, let’s explore how to evaluate code coverage with the OpenCover tool.
Read also:
How to Detect Vulnerabilities in Software When No Source Code Is Available
To show you an example of how to measure code coverage, we’ll use the following tools:
Since OpenCover doesn’t have a graphical interface, it can be launched and run only in the console. Its source code can be found on the official GitHub page.
To simplify work with OpenCover results, we usually use the free ReportGenerator tool. ReportGenerator converts code coverage results into readable files in various formats like HTML, Cobertura, and CSV. This tool works with reports created by OpenCover, dotCover, Visual Studio, NCover, Cobertura, JaCoCo, Clover, Gcov, and Lcov.
ReportGenerator will convert an XML file with code coverage results generated by OpenCover into a readable HTML report.
Both tools are available for installation through NuGet at the following links: OpenCover and ReportGenerator. We can easily install the required packages in Visual Studio using the NuGet package manager.
Once both tools are installed, we can directly interact with them from the console. They can be called with the following commands:
Code coverage is one of the most common metrics that helps software developers and quality assurance (QA) specialists assess testing efficiency. Evaluating code coverage helps you determine testing completeness, identify unnecessary code, and detect bugs.
In this article, we briefly explore what code coverage is, list the most popular tools for measuring it, and show how to evaluate the percentage of code coverage using the open-source OpenCover tool. This article will be helpful for developers, testers, and QA specialists who want to learn how to use the OpenCover tool for code coverage measurement.
Contents:
Code coverage is a measure of the percentage of program source code covered with unit tests. It can vary from 0% to 100%.
While test coverage measures the number of performed tests and monitors the quality of testing, code coverage indicates the percentage of lines of code covered with unit tests.
Why do you need to measure code coverage?
Evaluating code coverage is an essential part of test-driven development, which is based on writing tests before writing code. The idea is to reduce the amount of unnecessary code and avoid bugs.
Read also:
Test-Driven Development vs Behavior-Driven Development: What is the Difference?
One of the crucial benefits of analyzing code coverage is that it gives developers an understanding of whether there are enough tests in the unit test suite or whether they should create more tests to cover as many lines of code as possible.
Let’s take a look at the opportunities code coverage evaluation provides to developers:
Despite its advantages, measuring code coverage isn’t a silver bullet for checking code efficiency. One of the biggest challenges related to code coverage is interpreting the results.
Complete code coverage doesn’t mean that a project is fully tested. Even an empty method can be 100% covered, but this code is useless. 80% coverage is also a decent result, but there’s a risk that this 80% won’t cover the main scenario.
It’s essential to remember that code coverage results can’t point to flaws in the logic of your code. They also won’t report on the absence of features that should have been implemented.
Another crucial thing is to understand when you should concentrate on a high coverage percentage and when you shouldn’t.
It’s easier to cover more code with unit tests in small and mid-sized projects — and when using test automation. And it’s almost impossible to achieve high code coverage in a large-scale project with complicated logic that can lead to multiple errors. Google has spent more than a decade attempting to refine their coverage infrastructure and validate the most suitable approach.
Covering code with tests can be time-consuming and, therefore, costly. It’s reasonable to do so when you’re already familiar with the entire codebase, have information about recent and planned changes, and know specific requirements. However, if code is old and has gone through multiple changes — and if you don’t have clear requirements on how it should perform — covering it with tests will take lots of time and most likely won’t be very helpful.
Related services
Specialized Quality Assurance & Testing SolutionsCovering code with unit tests is as time-consuming as creating code. That’s why there’s a wide range of tools, both paid and free, that can accelerate the evaluation of code coverage.
Most tools for measuring code coverage are commercial, like NCover, dotCover, NCrunch, and NDepend. Also, Microsoft Visual Studio is equipped with a convenient code coverage tool with an attractive and intuitive user interface. However, it’s only available in the paid Visual Studio Enterprise version.
On the bright side, there are a fair number of efficient open-source code coverage tools. The most popular are:
In our projects, we mostly use OpenCover, which has proved its efficiency over the years and has numerous benefits:
Now, let’s explore how to evaluate code coverage with the OpenCover tool.
Read also:
How to Detect Vulnerabilities in Software When No Source Code Is Available
To show you an example of how to measure code coverage, we’ll use the following tools:
Since OpenCover doesn’t have a graphical interface, it can be launched and run only in the console. Its source code can be found on the official GitHub page.
To simplify work with OpenCover results, we usually use the free ReportGenerator tool. ReportGenerator converts code coverage results into readable files in various formats like HTML, Cobertura, and CSV. This tool works with reports created by OpenCover, dotCover, Visual Studio, NCover, Cobertura, JaCoCo, Clover, Gcov, and Lcov.
ReportGenerator will convert an XML file with code coverage results generated by OpenCover into a readable HTML report.
Both tools are available for installation through NuGet at the following links: OpenCover and ReportGenerator. We can easily install the required packages in Visual Studio using the NuGet package manager.
Once both tools are installed, we can directly interact with them from the console. They can be called with the following commands: