A wrapper around `anchor test` for computing test coverage
anchor-coverage
A wrapper around [anchor test] for computing test coverage
anchor-coverage requires a [patched] solana-test-validator (see below). The patch is known to work with Agave v3.0.8.
Steps to use
- Download, unzip, and untar a patched
solana-test-validatorfromsbpf-coverage's [Releases].
- Add the following to the
[profile.release]section of your Anchor project's root Cargo.toml:
debug = true
This tells Cargo to build with debug information.
- Run
anchor-coverageas follows:
anchor-coverage [ANCHORTESTARGS]...
This will create an sbftracedir directory with an LCOV file for each executable run.
- Run the following command to generate and open an HTML coverage report:
genhtml --output-directory coverage sbftracedir/*.lcov && open coverage/index.html
Known problems
anchor-coverage uses Dwarf debug information, not [LLVM instrumentation-based coverage], to map instructions to source code locations. This can have confusing implications. For example:
- one line can appear directly before another
- the latter line can have a greater number of hits
The following is an example. The line with the assignment to signer is hit only once. But the immediately following line is hit multiple times, because the instructions that map to it are interspersed with instructions that map elsewhere.
5 : pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
1 : let signer = &ctx.accounts.signer;
4 : let pubkey = signer.signer_key().unwrap();
11 : msg!("Signer's pubkey: {}", pubkey);
1 : Ok(())
1 : }
Troubleshooting
- If you see:
Line hits: 0
Check that you added debug = true to the [profile.release] section of your Anchor project's root Cargo.toml.
Links
- Useful reference re LCOV: gifnksm/lcov/src/record/mod.rs#L24-L206
anchor test]: https://www.anchor-lang.com/docs/references/cli#test
[from source]: https://docs.anza.xyz/cli/install#building-from-source
[patched]: https://github.com/trail-of-forks/sbpf-coverage