[SalesForce] Code coverage issues during production deployment — Number of test classes run changes

I am having an issue when loading a page, a controller and its test class to production environment. The deployment always fails due to a lack of code coverage (66% when 75% is the minimum), even though the controller is covered at 84%. Also, the code coverage for the production environment is estimated at 86%.

One thing that I have noticed is that there are only two non-managed test classes in the environment and about 1000 managed test classes. However, when I deployed the components only 16 test classes are run. I have done another deployment on the same environment where only 8 test classes were run. I am not sure what causes the difference in the number of test classes run. I am guessing this might be the cause of the code coverage issue.

Any help on these two items will be appreciated.

Thanks,

Waly.

Best Answer

UPDATE: SOLUTION (4/16/15)

  1. Clear Test History:

    (Setup->Apex Test Execution-> click link 'View Test History'->click button 'Clear Test Data')

    NOTE: The 'Clear Test Data' button completes the following 2 actions:

    • Deletes all entries from the ApexCodeCoverage table

    • Resets all the entries in the ApexCodeCoverageAggregate table to have 0 lines covered and all lines marked as uncovered This will result in seeing 0% code coverage in the Organizations estimation

  2. Manually clear entries for the 'ApexCodeCoverageAggregate' table:

    Via the Developer Console, execute the following query under the 'Query Editor' tab, using the 'Tooling API':

    SELECT Id FROM ApexCodeCoverageAggregate

    NOTE: Ensures there are no corrupt/invalid counts in the lines per class.

    A) Highlight every row returned and click the button 'Delete Row'

    B) Ensure there are no records in the 'ApexCodeCoverageAggregate' table by re-running the same query above in step 2 or hitting by the 'Refresh Grid' button as mentioned in the note above.

    NOTE: This process is asynchronous. Keep checking this by pressing the 'Refresh Grid' button in the Developer console until all rows are cleared.

  3. Recompile all Apex classes:

    Do this via 'Compile All Classes' link in UI (Setup->Develop->Apex Classes->click link 'Compile all classes')

  4. Run All Test

    Now with the tables/cache clear we re-run again via the 'Run All Tests' button in UI (Setup->Develop->Apex Classes->click button 'Run All Tests')

Full step-by-step details provided here:

Code coverage steps and considerations prior to deployments


ORIGINAL RESPONSE

I've been dealing with this issue for the past few weeks now, I've got a running answer listed here: Develop Console Code Coverage Issues (Winter 15' Release) - Production vs. All Test

I'm willing to bet that Winter 15' made some changes to Unit Test to include things they already should have, for example seeAllData=true test do not count towards code coverage, but in your estimated 86% you will see it counted. This only clarifies that there is a difference between perceived CC (Run All Test) and actual CC (Deployment to production)

There is an actual bug that SF has recognized but the patch is a few months out at least it sounds like. Checkout my post (link above) for more details as I have them

Also found this helpful article for clearing and getting accurate coverage amounts: https://help.salesforce.com/apex/HTViewSolution?urlname=Code-coverage-steps-and-considerations-prior-to-deployments&language=en_US

Related Topic