Code Coverage
Learn about the importance of code coverage.
We'll cover the following
Managers love numbers. The concept of code coverage, a term for how much code is unit-tested, is one that tickles the typical manager’s number fixation but ultimately leaves a bad taste when used for anything but educational purposes.
Code coverage
More specifically, code coverage is a measure of the percentage of code that our unit tests execute. We can find tools that do the dirty work of taking the measurements. Emma (which we show briefly here—it’s a free and easily installed Eclipse plugin) and Cobertura are examples of code-coverage tools.
Example: Coverage
class
Imagine that we have a Coverage
class containing only one method named soleMethod()
, and that soleMethod()
contains a single non=conditional statement. If we run a unit test that calls soleMethod()
, the statement is executed. The code coverage for soleMethod()
and the class (since Coverage
has no other methods), is 100%. If we run no unit tests that call soleMethod()
, the code coverage for Coverage
is 0%.
Next, consider that soleMethod()
contains an if statement with a single simple conditional and the body of the if statement is a single statement. Here’s an example of this code scenario:
Get hands-on with 1400+ tech skills courses.