Calculating Overall Code Coverage in Salesforce

Checking via Setup

To ensure reliability of the coverage calculation, complete the following steps:

  1. Configure “Apex Test Execution” via: Setup > Quick Find > “Apex” > Apex Test Execution. Click Options.
  2. Deselect Store Only Aggregated Code Coverage, then OK.
  3. Click View Test History. Then, Clear Test Data.
  4. 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.”
  5. 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.
  6. 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

Via Workbench:
https://workbench.developerforce.com/

  • For example, use the following line /services/data/v56.0/tooling/query/?q=SELECT+Coverage+FROM+ApexCodeCoverage