Print this page

Test class evaluated as part of the Organization’s Code Coverage

Knowledge Article Number 000188441
Description Test classes should not be evaluated in the overall code coverage numbers.  However, including code other than test methods in an @isTest annotated class will cause the lines in your test class to be incorrectly counted in the overall code coverage numbers.
Resolution There are two causes for them to be included in overall code coverage:

 
1. Including code other than test methods in an @isTest annotated class will cause these lines to be counted in the overall code coverage numbers:
  • Do not include non-test method code.  Classes defined with the @isTest annotation should only contain test methods.  That is described in our documentation.
  • Salesforce does not prevent the addition of non-test method code in your @isTest annotated test classes.
2. There are entries in the ApexCodeCoverageAggregate table for @isTest classes when they only contain valid test methods:
  • All @isTest annotated test classes that only contain test methods should have NumLinesCovered and NumLinesUncovered = null.
  • If they contain non-null values in these fields, they will appear in the Overall Code Coverage panel in the Developer Console (since the console issues a query to the Tooling API that filters on NumLinesUncovered != null) .
  • This is an error that can easily be resolved.

NOTE:  These values, and the resulting incorrect calculations, are only considered when executing Run All Tests, Estimate Code Coverage and when viewing results in the Developer Console.  While it can be off-putting to your development life-cycle, keep in mind that these values are not considered when code coverage is calculated during a deployment.  The value calculated at the time of deployment is accurate and does not use values stored in the ApexCodeCoverageAggregate table

You can verify whether or not your @istTest annotated test class is being incorrectly evaluated in the code coverage numbers by doing the following:
 
1. Setup | Developer | Apex Classes | Compile All Classes 
2. Setup | Developer | Apex Classes | Run All Tests
3. Log in to Salesforce via workbench.
4. Under Utilities, select REST Explorer, paste in the following REST URL and execute the following query (without line breaks): 
-  "/services/data/v29.0/tooling/query/?q=SELECT+ApexClassOrTriggerId,+ApexClassOrTrigger.Name,+NumLinesCovered,+NumLinesUncovered+FROM+ApexCodeCoverageAggregate++WHERE+NumLinesCovered+=+0+AND+NumLinesUncovered+!=+NULL+ORDER+BY+ApexClassOrTrigger.Name
5. Click on Show Raw Response to see the list Ids and names of classes and look for you test class name.
6. If the name of your @isTest annotated class is included in the response, then it is being evaluated in the code coverage numbers
7. If the class includes code other than valid test methods, we suggest you rework your @isTest annotated test class to remove non-test method code.
8. Run the above query again if necessary to verify that your test class is not contained in the response.

Example of problem code that would cause the @isTest annotated class to be incorrectly evaluated in the code coverage numbers:
 
@isTest
public class TestHelloWorld{
    @isTest
  static void TestSayYou() {
        //say you test method code
    }
static testmethod void TestSayMe() {
       //say me test method code
    }
 
    //Remove this.  This is an inner class.  Not a test method.
    public class HelloInnerClass {
        //HelloInnerClass non-test method code
    }
}

 

However, if an @isTest annotated class only contains test methods and yet the corresponding row in ApexCodeCoverageAggregate has NumLinesUncovered != null, this is an incorrect entry that should be deleted. To delete these entries:
 
1. Log in to Salesforce via workbench.
2. Under Utilities, select REST Explorer, paste in the following REST URL and execute the following query (without line breaks):
- /services/data/v30.0/tooling/query/?q=SELECT+ApexClassOrTriggerId,+ApexClassOrTrigger.Name,+NumLinesCovered,+NumLinesUncovered+FROM+ApexCodeCoverageAggregate+WHERE+(ApexClassOrTriggerId+=+'<insert the affected test class Id here without angular brackets>'+OR+ApexClassOrTriggerId+=+'<insert additoinal class Ids here, and so on>')
3. In the response, click [records] -> [ItemX] -> [attributes]
4. Open the URL in a new browser tab
5. Click  'DELETE' HTTP at the top of the form.
6. Click Execute.
7. You should receive 'HTTP/1.1 204 No Content'
8. Go back to the previous tab and repeat for each additional [Item] folder under [records]
9. After deleting all offending rows, Run All Tests and confirm that these test classes ae not included in the Overall Code Coverage panel in the Developer Console.


NOTE:  These values, and the resulting incorrect calculations, are only considered when executing Run All Tests, Estimate Code Coverage and when viewing results in the Developer Console.  While it can be off-putting to your development life-cycle, keep in mind that these values are not considered when code coverage is calculated during a deployment.  The value calculated at the time of deployment is accurate and does not use values stored in the ApexCodeCoverageAggregate table.




promote demote