As any good Salesforce developer will tell you, Apex code coverage is important — and not just because it’s required to deploy. Code coverage indicates how many executable lines of code in your classes and triggers have been exercised by test methods. Having quality methods to test your triggers and classes is invaluable to catch issues before production and deliver a solid experience for your users. So, what’s new?
Within the CLI and VS Code Extensions, we’re changing the code coverage info so it shows the same coverage data used when deploying. This will give you consistent info no matter where you work, so you don’t have surprises along the way. We’re also simplifying it so you have less info overall to process. Even better!
You can view Apex code coverage in several ways: from the Setup menu within an Org, Dev Console, a SOQL query, the Salesforce CLI or the Salesforce Extensions in VS Code. As many have noticed, the information represented looks a little different depending on where you look. This can lead to confusion and disruptions in your development process if the code coverage results appear inconsistent. For example, while developing a new class, the team looks at the code coverage results in the CLI. They see the code coverage is 94% and believe enough tests are in place. However, when the deployment is attempted, they discover the actual percentage of coverage for that class across the Org is only 58%, which blocks their deployment. Let’s look a little deeper about why the information varies:
- The Dev Console displays the overall Org-wide coverage along with the percent covered for each Apex class across the whole Org. The Org-wide coverage information is what determines if you have adequate coverage to deploy or package.
- The CLI and Extensions provide a granular view of the test, showing you each method and class being covered by the test you ran (not the whole Org). Because the stats apply to the current test run, the class coverage may not match what’s seen in the Org or Dev Console since those are showing coverage across the Org, regardless of which test just ran.
- The CLI and Extensions also calculate an aggregate “Test Run Coverage” number that has no relationship to the overall Org-wide coverage.
Here’s a mock-up of the streamlined output when running the
Percentages for class coverage and Org-wide coverage will be updated in the human readable format and the output files to match what is in the Org — which is the source of truth for Code Coverage.
If you’d like to see all the details of the changes, check out the full spec on Github.
Happy coding and covering!
About the Author
Stephanie Maddox is the Product Manager for the VS Code Extensions. She joined ExactTarget in March 2010 and became an official part of Salesforce in July 2013 as a part of that acquisition. During her career at Salesforce, she has focused on Customer Success and Product Management. She holds a degree in Computer Science from Ball State University and spent the first 8 years of her career as a developer.