Solidity Coverage in VS Code with Foundry

Yeah, I know you’ve probably heard about the hype by now. People are flocking to Foundry and there’s nothing you can do about it. So, strap in, there’s been a new release which brings coverage reporting!

Install / Upgrade Foundry

First, make sure you’re on the latest version of Foundry. If you don’t have it, get it here.

If you do have it installed already, get the latest version by running:

$ foundryup

View Coverage

To get a coverage report, it all starts with:

$ forge coverage

By default this will display a summary, which will look like this:

It’s already quite nice, but you can change the output via the --report flag. In order to get the coverage displayed nicely in VS Code, we need to use the LCOV reporter. So, run:

$ forge coverage --report lcov

You should now have a file in your project directory. LCOV files can be parsed by many tools / extensions. In the case of VS Code I recommend Coverage Gutters.

  1. Install the Coverage Gutters extension.
  2. Open the file you want to see coverage results for.
  3. Open the command palette in VS Code (CMD+SHIFT+P or CTRL+SHIFT+P by default) and type “Display Coverage”, you should see the option “Coverage Gutters: Display Coverage“, select it.
  4. Now, your file should be nicely colored line-by-line showing if a line has been hit or not (green for hit, red for missed).

Obviously, there are some issues present. The constructor is definitely being hit in my tests, but it’s showing as a miss! Coverage in Foundry is still very much a work-in-progress, but it’s great that we have an initial version so we can start setting up our tooling and processes around it. Knowing the Foundry team, this feature will quickly blossom into another example of what the premier Solidity testing framework has to offer.

Example Repos

Subscribe to Devan Non
Receive the latest updates directly to your inbox.
This entry has been permanently stored onchain and signed by its creator.