We hired a consulting firm to do some Apex for us. I've been reviewing their code and realized the code coverage was currently at 12% - so I've been working on getting the code coverage up (currently at 68%). However, I ran across an Apex Class named 'TestUtils' that essentially creates test records for the other unit tests in the org. That is great except this 'TestUtils' class doesn't have a unit test and I'm not sure how to implement one. If I'm able to exempt this class from code coverage or write a unit test for it - I'd be up to 93% code coverage.
1 Answer
000188441 explains this for you:
There are two causes for [unit test classes] to be included in overall code coverage:
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.
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
Also, you can sometimes end up with erroneous values. It's explained how to fix it:
[...] 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:
- Log in to Salesforce via workbench.
- 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+=+''+OR+ApexClassOrTriggerId+=+'')
- In the response, click [records] -> [ItemX] -> [attributes]
- Open the URL in a new browser tab
- Click 'DELETE' HTTP at the top of the form.
- Click Execute.
- You should receive 'HTTP/1.1 204 No Content'
- Go back to the previous tab and repeat for each additional [Item] folder under [records]
- 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.
@IsTest
? If so, it actually doesn't count against you and is just a bug that the platform tells you it does.