Test Coverage Metrics

Test coverage metrics answer the question: how much of our code is executed by our tests?

2 main metrics:

Code coverage (text coverage)=Lines of code executedTotal number of lines\text{Code coverage (text coverage)}=\frac{\text{Lines of code executed}}{\text{Total number of lines}}

Branch coverage=Branches traversedTotal number of branches\text{Branch coverage}=\frac{\text{Branches traversed}}{\text{Total number of branches}}

Code coverage

The most commonly used metric.

The downside of using it that code can be "shuffled" to trick the metric into increasing. This can sometimes make code less readable.

Branch coverage

Code can't be simplify shuffled without refactor, as branches will be examined whether one line or multiple.

The downside, which covers all code coverage metrics, is that tests without assertions will be counted as branch traversal.

Khorikov (2020) (pg. 8-15)


Vladimir Khorikov. Unit Testing: Principles, Practices, and Patterns. Manning, Shelter Island, NY, 2020. ISBN 978-1-61729-627-7.