Code Coverage
Measuring code coverage uses tools to identify lines of code that are and aren't executed by tests. Code coverage stats can give us an idea of how much of a contract is actually tested by its tests.
Mutation testing is another form of coverage testing. See Mutation Testing.
In rust projects the cargo-llvm-cov
tool can be used to generate coverage stats, HTML reports, and lcov files that IDEs will load to display the coverage in the code editor.
Install cargo-llvm-cov
before proceeding with the other commands.
cargo install cargo-llvm-cov
How to Get Coverage Statsβ
Run the test subcommand that will run the tests and output the stats per file.
cargo llvm-cov test
How to Generate a Coverage Report with Codeβ
Run the test subcommand that will run the tests and output a set of HTML files showing which lines of code are covered.
cargo llvm-cov test --html --open
The output of the command will indicate where the HTML file has been written. Open the file in a browser.
How to Generate an LCOV File for IDEsβ
Run the test subcommand that will run the tests and output a single lcov.info
file.
cargo llvm-cov test --lcov --output-path=lcov.info
Load the lcov.info
file into your IDE using it's coverage feature. In VSCode this can be done by installing the Coverage Gutters extension and executing the Coverage Gutters: Watch
command.
Measuring code coverage in fuzz tests requires different tooling. See Fuzzing.
Guides in this category:
ποΈ Unit Tests
Unit tests are small tests that test smart contracts.
ποΈ Mocking
Mocking dependency contracts in tests.
ποΈ Test Authorization
Write tests that test contract authorization.
ποΈ Test Events
Write tests that test contract events.
ποΈ Integration Tests
Integration testing uses dependency contracts instead of mocks.
ποΈ Integration Tests with Mainnet Data
Integration testing uses dependency contracts instead of mocks.
ποΈ Fuzzing
Fuzzing and property testing to find unexpected behavior.
ποΈ Differential Tests
Differential testing detects unintended changes.
ποΈ Differential Tests with Test Snapshots
Differential testing using automatic test snapshots.
ποΈ Code Coverage
Code coverage tools find code not tested.
ποΈ Mutation Testing
Mutation testing finds code not tested.