1

How do I measure test coverage for an Anchor program?

  1. If I write my tests in typescript, then there are no code coverage reports
  2. If I write my tests in Rust (similar as specified here), and use the command cargo llvm-cov nextest --html --output-dir ./coverage then the coverage report does not include any code that was executed within the Solana test network/client/VM (not sure what's the correct term here).
  3. I could try splitting out all functionality from each method on the Solana program into independent functions and test them in isolation, without running them through the test validator, but then I have no way of testing and seeing the coverage on the Anchor account definitions.
1
  • Same question. Does anyone know how to do test coverage on Anchor?
    – 7Haifai
    Commented Mar 10, 2023 at 7:20

1 Answer 1

2

I think this is the wrong way to think of testing in web3. Most of your important tests will be integration tests with the chain, not unit tests on the protocol side. That is, your Typescript tests that ultimately call the Rust instructions are the bulk of your testing coverage.

Since there's no way to see what code paths in the protocol your integration tests cover, I don't think it's possible to see your coverage. Many organizations that are concerned with coverage will also implement fuzzing to ensure that all possible code paths are followed.

1
  • 1
    In the EVM world, getting test coverage reports for the Solidity code with tools like hardhat-coverage is a simple thing to accomplish; pretty much all protocols measure their test coverage in one way or another, so I do not believe that this is the wrong way of approaching tests in web3. Especially when dealing with immutable contracts, nothing should be left to chance. Solana not providing an ability to gain such introspect in the code execution is a huge problem in my opinion. Commented Sep 1, 2022 at 14:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.