ApexCodeCoverage

Represents code coverage test results for an Apex class or trigger.

Version 

Introduced in API version 29.0.

Supported SOAP API Calls 

delete(), describeSObjects(), query(), retrieve(), update()

Supported REST API Methods 

DELETE, GET, HEAD, PATCH, Query

Special Access Rules 

  • In API version 49.0 and later, users must have the View Setup and Configuration and View All Data permissions to access this object.

Fields 

ApexClassOrTriggerId 

  • Type: reference

  • Required: No

  • Properties: Filter, Group, Sort, Update

  • Relationship name: ApexClassOrTrigger

  • Relationship type: polymorphic

  • Refers to: ApexClass, ApexTrigger

ApexTestClassId 

  • Type: reference

  • Required: No

  • Properties: Filter, Group, Sort, Update

  • Description: The ID of the test class.

  • Relationship name: ApexTestClass

  • Relationship type: relationship

  • Refers to: ApexClass

Coverage 

  • Type: complexvalue

  • Required: No

  • Properties: Nillable

  • Description: 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.

Coverage includes these fields:

  • coveredLines
  • namespace
  • uncoveredLines

NumLinesCovered 

  • Type: int

  • Required: No

  • Properties: Filter, Group, Nillable, Sort, Update

  • Description: The number of covered lines.

NumLinesUncovered 

  • Type: int

  • Required: No

  • Properties: Filter, Group, Nillable, Sort, Update

  • Description: The number of uncovered lines.

TestMethodName 

  • Type: string

  • Required: No

  • Properties: Filter, Group, Nillable, Sort, Update

  • Description: The name of the test method.

Usage 

To query for code coverage, specify an Apex class, test class, or both. The returned JSON or XML object contains 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 ApexClassOrTriggerId = ‘01pD000000066GR’ 
4AND ApexTestClassId = ‘01pD000000064pu’

For per-class code coverage, the query is:

1SELECT Coverage
2FROM ApexCodeCoverage 
3WHERE ApexClassOrTriggerId = ‘01pD000000066GR’

In this case, multiple rows can be returned, since there could be multiple test classes that cover the same test class.

Note

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 is: {2,9,11},{3,4,5,6}. The missing lines (1, 7, 8 and 10) aren’t executable.

Code coverage percentage is a simple calculation of the number of covered lines divided by the sum of the number of covered lines and the number of uncovered lines. For example, to calculate code coverage percentage in SOAP:

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 + "%.");

If a single deployment has over 2,000 Apex classes, ApexCodeCoverage objects for the deployed classes are deleted even if the deployment fails or is rolled back. ApexCodeCoverageAggregate objects aren’t affected.

Note

DID THIS ARTICLE SOLVE YOUR ISSUE?
Let us know so we can improve!