maj 222015

Salesforce – How can I get the code coverage of a class / org?

To get code coverage of a class or all classes you can use Tooling API on objects like:

  • ApexCodeCoverage – contains coverage per class per test class
  • ApexCodeCoverageAggregate – contains coverage per class for all test classes
  • ApexOrgWideCoverage – contains coverage overall for all classes in the org
The best way is run queries using Developer Console

1. How many lines are covered for a specific class or trigger:
SELECT NumLinesCovered, NumLinesUncovered 
FROM ApexCodeCoverage

or

SELECT ApexClassOrTrigger.Name, NumLinesCovered, NumLinesUncovered 
FROM ApexCodeCoverageAggregate 
WHERE ApexClassOrTrigger.Name = 'SampleClassName'

 

2. Which lines are covered for a specific class or trigger:
SELECT Coverage 
FROM ApexCodeCoverage
or
SELECT Coverage 
FROM ApexCodeCoverage 
WHERE ApexClassOrTriggerId = <class-id>

 

3. The current org-wide coverage:
SELECT PercentCovered 
FROM ApexOrgWideCoverage

 

4. How many lines are covered for a specific class or trigger:

SELECT NumLinesCovered, NumLinesUncovered 
FROM ApexCodeCoverage 
WHERE ApexClassOrTriggerId = 'ClassID'

 

5. Other usefull queries:

SELECT ApexClassOrTriggerId, ApexClassOrTrigger.Name, NumLinesCovered, NumLinesUncovered 
FROM ApexCodeCoverageAggregate 
WHERE ApexClassOrTriggerId != NULL AND ApexClassOrTrigger.Name != NULL 
AND (NumLinesCovered > 0 OR NumLinesUncovered > 0) AND NumLinesCovered != NULL 
AND NumLinesUncovered != NULL ORDER BY ApexClassOrTrigger.Name

 

SELECT ApexClassOrTriggerId, ApexClassOrTrigger.Name, NumLinesCovered, NumLinesUncovered 
FROM ApexCodeCoverageAggregate 
ORDER BY ApexClassOrTrigger.Name
Tagged with , , , , , , 0 komentarzy

Dodaj komentarz

Your email address will not be published. Please enter your name, email and a comment.