0

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.

  • Is the class already annotated with @IsTest? If so, it actually doesn't count against you and is just a bug that the platform tells you it does. – Adrian Larson May 22 '17 at 16:33
  • TestUtils are meant to be a single source of data generator for all your different scenarios. If you use the methods of the test utils for generating test data and if its within test.start and stop even the test utils class code coverage will be taken care of. – RedDevil May 22 '17 at 16:34
  • Make sure that you run all test classes in your org in order to cover the testutil class. – Durgapaavan k May 22 '17 at 17:00
3

000188441 explains this for you:

There are two causes for [unit test classes] 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


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:

  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+=+''+OR+ApexClassOrTriggerId+=+'')
  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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.