Print this page

Winter '14 - How can I get the code coverage of a class using the API? (186)

Knowledge Article Number 000182228
There are three setup entities that contain code coverage information:
a) ApexCodeCoverage contains coverage per class per test class
b) ApexCodeCoverageAggregate contains coverage per class for all test classes
c) ApexOrgWideCoverage contains coverage overall for all classes in the org
By using the Tooling API we can get the following information:
1) How many lines are covered for a specific class or trigger:
SELECT NumLinesCovered, NumLinesUncovered
FROM ApexCodeCoverage
WHERE ApexClassOrTriggerId = <class-id>
2) Which lines are covered for a specific class or trigger:
SELECT Coverage
FROM ApexCodeCoverage
WHERE ApexClassOrTriggerId = <class-id>
3) The current org-wide coverage:
SELECT PercentCovered
FROM ApexOrgWideCoverage
To test these queries you may use Workbench ( as follows:
1) Go to Utilities | REST Explorer
2) Select GET
3) Enter the following URL:
where YOUR_QUERY should be the query to execute
4) Click "Show Raw Response".
5) For instance, if SELECT PercentCovered FROM ApexOrgWideCoverage is used, this is the format of the response:
  "size" : 1,
  "totalSize" : 1,
  "done" : true,
  "records" : [ {
    "attributes" : {
      "type" : "ApexOrgWideCoverage"
    "PercentCovered" : 0
  } ],
  "queryLocator" : null,
  "entityTypeName" : "ApexOrgWideCoverage"

promote demote