Test coverage is a measurement that helps evaluate the completeness of tests. While "line coverage" is the most popular coverage measurement, achieving a perfect score of 100% does not mean that the tests are complete.
In fact, there is no universal test coverage metric that can help indicate whether tests for given smart contracts are complete. Instead, different coverage metrics measure test completeness from very narrow points of view. In this guide, we will list what coverage metrics exist, which ones can be measured with tools and which metrics could be promising for smart contract developers.
Test coverage is useful in indicating what additional tests could be written. For example, if line coverage is 90%, a coverage tool could be used to show which lines of the code are not tested in any tests. This would give a concrete idea for developers on how to add tests that are complementary to the existing test suite.
Another way to use test coverage is to enforce minimum guidelines for a repository. For example, it's possible to encode a minimum coverage requirement in continuous integration testing which will help developers realize that they have not added enough tests.
The danger with using a minimum coverage guideline is that this can be misconstrued to indicate "completeness". As discussed above, coverage metrics should only be used to suggest additional possible tests rather than as a tool for obtaining confidence.
Line coverage is the most basic method to assess test coverage. Line coverage is simply defined as the number of lines of code that execution passes through in tests divided by the total number of lines of code in the test suite.
Since this method is so common, tools exist to measure line coverage in Solidity Test Coverage tool.
We discuss how to measure coverage for specific testing techniques in How to Test Smart Contracts.