I am trying to get the test coverage for our Smart Contracts. Couple of questions I have.
- While running the coverage command, it doesn't include all the test cases in the folder, is it because we have written test cases in a modular form, i.e. there are folders inside folders and then test cases, so that the structure remains clean when the codebase grows.
The files included in the report:
The actual files I have:
- While using the
--report summary
flag it creates a JSON file that is not practically readable to a human eye, what is the best way to extract the details given in that JSON file?
JSON File :
- Lastly, are there some docs, etc. for more information on the usage of forge coverage, as the foundry book is pretty limited.
Best Answer
The answers below refer to forge 0.2.0, the latest release at the time of the writing.
1. Running tests to get the coverage
The coverage operation executes all the tests in the test files, including those in subfolders of the
test
folder. But you need to put the tests in thetest
folder (or subfolders) and the source to be tested in thesrc
folder (or subfolders).When working correctly, what you see in the report generated by the
forge coverage
command are the results of testing the source code files present in thesrc
folder running the tests in thetest
folder.You should not see the file containing the tests listed in that report.
In your case, looking at the screenshot, I see the test cases files (
.t.sol
) listed in the report, so it means Forge thinks those files must also be included in the coverage report. And as a side note, nothing is executing tests over the test files, so this is the reason why you get 0% for those files.You need to fix your project structure: remove those test files from being the target of the coverage, put them under a dedicated
test
folder, move the source code under asrc
folder, change the importing path accordingly to find the source.sol
contracts again.You project should look something like this:
Using the
src
folder as the root folder for your code and thetest
folder on the same level for the test case is a best practice, but you can change that editing thefoundry.toml
configuration file; still, I suggest sticking with the best practices to have fewer problems with the repo maintenance.See more config options at the official repo.
2. Coverage reporting options
I'm not sure how you produced that JSON, but the coverage
--report
option works like this:3. Documentation and tutorials
Official documentation is not super detailed, but you can find on the Internet articles like this one from Rohanzarathustra, that let you play around with all the possibilities of the coverage command.
Please consider the program is labelled
0.2
for a reason, so expect improvements, changes and breaking changes in the future. ;)