Calculating unit test coverage
Unit tests tell you whether your application code behaves as expected. Unit tests are important to maintain code quality and catch errors early in the development cycle. However, this goal is at risk if you do not write enough unit tests to test your application code, or if you have not tested all possible input conditions in the test cases and the exception paths. To measure the quality and adequacy of your test cases, you need to calculate the coverage of your test cases. In simple terms, coverage tells you what percentage of your application code was touched by running your unit tests. There are different measures to calculate coverage:
- Number of lines covered
- Number of branches covered (created using the
if
,else
,elseif
,switch
, andtry
/catch
statements) - Number of functions covered
Together, these three measures give a fair measurement of the quality of your unit tests. There are many code coverage tools for Java. In this chapter, we will take a look at an...