#coverage #anchor #solana #anchor-coverage

bin+lib anchor-coverage

A wrapper around anchor test for computing test coverage

1 unstable release

Uses new Rust 2024

new 0.1.0 Apr 10, 2025

#279 in Magic Beans

Download history 82/week @ 2025-04-05

82 downloads per month

AGPL-3.0

27KB
599 lines

anchor-coverage

A wrapper around anchor test for computing test coverage

Steps to use

  1. Install the Agave validator from source after adding the following to the [patch.crate-io] section near the end of its Cargo.toml:

    solana-sbpf = { git = "https://github.com/trail-of-forks/sbpf-coverage" }
    
  2. Add the following to [profile.release] section of your Anchor project's root Cargo.toml:

    debug = true
    

    This tells Cargo to build with debug information.

  3. Run anchor-coverage as follows:

    anchor-coverage [ANCHOR_TEST_ARGS]...
    

    This will create an sbf_trace_dir directory with an LCOV file for each executable run.

  4. Run the following command to generate and open an HTML coverage report:

    genhtml --output-directory coverage sbf_trace_dir/*.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 reason is that multiple instructions can map to the same source line. If multiple instructions map to the latter source line, it can have a greater number of hits than the former.

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 is: {}", 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.

Dependencies

~3.5–5MB
~98K SLoC