Calculating Overall Code Coverage in Salesforce
These notes are a worked example based on a Salesforce help article, called "Calculate overall code coverage in Salesforce."
Checking via Setup
To ensure reliability of the coverage calculation, complete the following steps:
- Configure “Apex Test Execution” via: Setup > Quick Find > “Apex” > Apex Test Execution. Click Options.
- Deselect Store Only Aggregated Code Coverage, then OK.
- Click View Test History. Then, Clear Test Data.
- Go to “Apex Classes” via: Setup > Quick Find > “Apex” > Apex Classes. Click Compile all classes. The process may take a few minutes. Once complete the UI will say “Compilation Complete.”
- Go back to “Apex Test Execution” then Select Tests, select (“All Namespaces” to include managed packages, or “My Namespace” for just unmanaged Apex). When running tests for “All Namespaces” the process can take several minutes - in one org it took over 40 minutes.
- Go back to “Apex Classes” then click “Estimate your organization’s code coverage.”
Checking via the Tooling API
Via Developer Console
- Check how many lines are covered for a specific class or trigger:
SELECT Id, ApexTestClassId, TestMethodName, NumLinesCovered, NumLinesUncovered FROM ApexCodeCoverage WHERE ApexClassOrTriggerId = '01XXXXXXXXXXXXX'
- Check which lines are covered for a specific class or trigger:
SELECT Coverage FROM ApexCodeCoverage WHERE ApexClassOrTriggerId = '01XXXXXXXXXXXXX'
- The current org-wide coverage:
SELECT PercentCovered FROM ApexOrgWideCoverage
- For example, use the following line