Newer Version Available
ApexCodeCoverage
Supported SOAP API Calls
describeSObjects(), query(), retrieve()
Supported REST API HTTP Methods
Query, GET
Special Access Rules
In API version 49.0 and later, users must have the View Setup and Configuration permission to access this object.
Fields
| Field | Details |
|---|---|
| ApexTestClassId |
|
| TestMethodName |
|
| ApexClassorTriggerId |
|
| NumLinesCovered |
|
| NumLinesUncovered |
|
| Coverage |
|
Usage
To query for code coverage, specify an Apex class, test class, or both. The returned JSON or XML object will contain two lists of integers: one for covered and one for uncovered lines.
The following example SOQL query retrieves code coverage results for a specific class or trigger covered by a specific test class:
1SELECT Coverage
2FROM ApexCodeCoverage
3WHERE ApexClassOrTrigger = ‘01pD000000066GR’
4AND ApexTestClass = ‘01pD000000064pu’For per-class code coverage, the query would be:
1SELECT Coverage
2FROM ApexCodeCoverage
3WHERE ApexClassOrTrigger = ‘01pD000000066GR’As noted above, Coverage is returned as two lists of integers. The first is the covered lines, and the second is the list of uncovered lines. If a line is missing from both lists, the line isn’t executable and doesn’t require coverage. For example, if the covered lines are 2, 9, and 11, and uncovered lines are 3, 4, 5, and 6; the result would be: {2,9,11},{3,4,5,6}. The missing lines (1, 7, 8 and 10) aren’t executable.
1ApexCodeCoverage acc = null; //Query for an ApexCodeCoverage object
2Coverage coverage = acc.coverage;
3int[] covered = coverage.coveredLines;
4int[] uncovered = coverage.uncoveredLines;
5int percent = covered.length / (covered.length + uncovered.length);
6System.out.println("Total class coverage is " + percent + "%.");