The Silver Lining

Lessons & Learnings from a salesforce certified technical architect.

Salesforce: Different percentage unit test code coverage in different environments

with 3 comments

Spot the difference.

Many people are finding that their tests are reporting different degrees of test-coverage in different environments. There are a few things to check if you are getting inconsistent results but there’s a new bug in the wild. Before you assume you have the bug make sure that you’ve:

  1. ‘Run All Tests’ in each environment. This will tell you a few things viz.
    • Perhaps there are tests failing that are bringing coverage in that environment down.
    • There are some tests that only fail when run in the browser e.g. MIXED_DML_EXCEPTION will not rear it’s head through the IDE.
  2. Click the ‘Compile all classes’ link above the Setup > Develop > Apex Classes view. I’m not sure when this lil’ bugger first appeared but it’s darn useful. Essentially it makes sure that all the dependencies in your code are fulfilled e.g. if page A uses controller B that in turn refers to utility class C it’ll make sure each of those pieces exist and work (as far as compilation goes at least).
  3. Double-check your test classes to make sure they’re not data dependent. If they are and you have different types/amounts of data in your respective environments it’s 100% feasible that the test coverage will be different.

Now if you’ve checked all of the above you might have been afflicted by a new bug which, for some reason, counts braces, whitespace (and more!) as being uncovered by your tests. This is preposterous of course and to fix it simply remove all test history from the deranged environment. Re-running the test and/or deploying them should now be back to normal!

About these ads

Written by Wes

November 30, 2011 at 11:01 pm

3 Responses

Subscribe to comments with RSS.

  1. [...] Salesforce: Different Percentage Code Covered in Different Environments @wesnolte A few things to check if you are finding that your tests are reporting different degrees of test-coverage in different environments. [...]

  2. The MIXED_DML_EXCEPTION thing in the IDE is being fixed. That said, you should be running your tests in the Apex Test Execution page now :)

    Data dependent tests will be a thing of the past in #spring12 so long as you don’t disable the new data silo in your tests with @IsTest(SeeAllData=true). Otherwise, SOQL queries and suchlike will only see data created in the test.

    Rich Unger (@rich_unger)

    January 18, 2012 at 6:38 pm

    • Dude that’s some pretty exciting news! Thanks for the update.

      Wes

      January 19, 2012 at 11:34 am


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 2,099 other followers

%d bloggers like this: