Unit Test Coverage: Concepts and Techniques


Test coverage is a measure of the degree to which application code is executed when test code is run. Test code may enhance test coverage by increasing the branch coverage of the application code. Branch coverage is the ratio of the number of executed branches in an application code’s control structure and the total number of branches in control structures. Control structures include selection statements and iteration statements. Selection statements include the if, if else, and switch statements. Iteration statements include the do, for, for each, and while statements.

You can think of branch coverage qualitatively and quantitatively. Its quantitative measurement is given by the following formula:

Branch coverage=Branches traversedTotal number of branches\mathsf{Branch\ coverage} = \dfrac{\mathsf{Branches\ traversed}}{\mathsf{Total\ number\ of\ branches}}

If the test code invokes a branch coverage of 100%, we know that our test cases are comprehensive. This, however, doesn’t guarantee that the code caters to all behavior. Testing all application code is not necessary given that some application code is trivial from a testing perspective. Trivial application code includes entities, class properties, and repositories in a web application.

Get hands-on with 1200+ tech skills courses.