Salesforce APEX Code Coverage Visualizer for Visual Studio Code
This extension shows a summary of the code coverage for all APEX classes in the current project. The extension relies on the official Salesforce Extensions test runner which creates the necessary files used to show the code coverage.
It works with both Dark and Light themes
Make sure you have configured the preferences on the Salesforce extension to retrieve code coverage on test execution. To do this, go to Visual Studio Code menu File->Preferences->Settings and enter "coverage" in the search box, and then from the search results enable the Retrieve-test-code-coverage setting:
After that, just execute some tests as you normally would from the Test view:
Once the test coverage has been downloaded by the Salesforce CLI, you can now see the code coverage for a specific Apex file in the status bar, and also the overall code coverage in a new window:
Coverage in Status Bar
The indicator in the status bar will only appear if the Apex class has tests coverage downloaded for such class. You can see three indicators, depending on the coverage percentage:
You can get to this window by either clicking on the coverage indicator on the status bar, or by executing the SFDX: Show Code Coverage from the command palette:
It is possible to filter down the test results to show only tests that are below the required 75% coverage, to allow the developer to focus on those tests that require inmediate action. Also, it is posible to filter further down on the name of the apex file:
It is possible that the tests you write might contrigute to the coverage of other classes that are not in your project, by default the coverage viewer shows only classes that are present on the current project, but you can disable this and show all other classes as well.
Coverage Side Panel
The coverage side panel shows by default on the Test view, but it is possible to move it to any other place inside the Visual Studio Code layout:
The sidebar also allows to filter down the classes by clicking on the commands on the title bar:
Test Coverage Contribution
It is possible to know what test classes and methods are contributing to the overall coverage percentage of a class by expanding the chevron icon on the right of the class:
This extension depends on the code coverage information retrieved from SFDX using the standard SFDX extensions for code. This extension cannot show coverage for classes if the coverage for those classes is not retrieved. For big projects, with lots of classes and tests, the SFDX extension sometimes does not retrieve all code coverage for all the classes, so it is possible that you will only see coverage for certain classes while other classes will show no coverage. This is not an issue of this extension, but rather something related with the retrieval of the coverage by the SFDX standard extension.
Bugs and Feedback
To report issues with Salesforce Extensions for VS Code, open a bug on GitHub. If you would like to suggest a feature, create a feature request on GitHub.
Thanks to Kevin Poorman (@codefriar) for its contribution to the idea of showing the test coverage in a sidebar panel for easier reference. Kevin and I did this enhancement on an episode of Salesforce Developer Code Live, which you can find here.
This project is licensed under the terms of the MIT license.