[SalesForce] Low code coverage, when production and new test class to deploy are over 80%

I am having an issue, for the last couple of days and I dont know how to fix it.

I have run all the test classes in production, when I open my developer console, the overall code coverage is 79% (of all production). I have created a test class that covers 2 classes that weren't covered before, raising the code coverage for 1 class to 100% and the second class to 92%
I am trying to deploy this test class, but I am getting an error saying that overall code coverage is 73%.

This doesn't have any sense, as this new test class are over 90%, if my overall is 79% this should actually increase my coverage not decrease it.

Is there anything that I am missing? or is there another way to find out overall code coverage, and my developer console is giving me the wrong overall?

Best Answer

This is a very general question, so I can only respond with a generic answer.

You might verify the following:

  • Are the two classes to be tested identical on production and sandbox/devOrg?
  • since it's the overall coverage: do other classes differ?
  • do you use seealldata=true? If so, coverage is dependent on data and other parts of your code might be triggered.
  • If possible, try to use seealldata=false
  • make sure that all triggers are identical. During tests and DML triggers might fire.
  • try to deploy all trigger, classes and tests at the same time (e.g. ctrl-select the classes and triggers root nodes in eclipse and then deploy)
  • do you use any hardcoded ids? E.g. for recordtype detection, etc?
  • is the api version in the xml files the same for all classes and triggers to their counterpart on production?
  • do you have identical workflows on sand and prod? Also thy can fire and cause other tiggers to be invoked.
  • try/catch can be very tricky: if your code fails inside a catch, the tests won't fail but silently skip large parts of your code