+ Start a Discussion
asadim2asadim2 

Strange Code Coverage error in deployment

The following 2 points should explain everything:

1. Code coverage in production is 74% (cleared all tests, then re-ran all to make sure). When deploying a single field (some dummy test field) from the sandbox we get this error:
Your code coverage is 63%. You need at least 75% coverage to complete this deployment.

2. Production is on Summer '15. Sandbox is not. Regardless, we hardly have multi-line code anywhere (maybe 1% of all Apex code).

Why is production code coverage falling to some obscure value during deployment of changesets?
Best Answer chosen by asadim2
asadim2asadim2
Turns out I had to manually reset the test coverage per this article:
https://help.salesforce.com/apex/HTViewSolution?urlname=Code-coverage-steps-and-considerations-prior-to-deployments&language=en_US

I am now seeing 63% coverage in production (under Apex Classes > Estimate Code Coverage), although Developer Console is still showing 74%.

All Answers

pconpcon
Are you using SeeAllData anywhere in any of your tests?  Do you have any user level flags that may affect the test output (skip large sections of code) that is enabled in prod but disabled in dev?
asadim2asadim2
Yes we have seeAllData=true on many of the test classes. There are no flags to skip any code anywhere.

Please keep in mind, that we are seeing 2 very different values for code coverage in the same org (production). I assume what's in the dev/sandbox org is irrelevant.
pconpcon
having seeAllData could be the culprit.  If your tests are expecting data that exists in your dev instance but not your prod instance then that will cause a change in your test coverage.  There is almost no reason to use seeAllData in your tests now.
asadim2asadim2
Correct. Except that, the dev/sandbox org should technically play zero role here, right? All I am deploying from it is a single dummy field.

This is an old org, hence the seeAllData=true in the test cases.
asadim2asadim2
Turns out I had to manually reset the test coverage per this article:
https://help.salesforce.com/apex/HTViewSolution?urlname=Code-coverage-steps-and-considerations-prior-to-deployments&language=en_US

I am now seeing 63% coverage in production (under Apex Classes > Estimate Code Coverage), although Developer Console is still showing 74%.
This was selected as the best answer
pconpcon
Incorrect.  Deploying a field still requires a full test run.  If the tests rely on data that does not exist then it would not do coverage.  Take the following code as an example:
 
trigger caseUpdate on Case (before insert) {
    List<Contact> contactList = [select Id from Contact where ContactType__c = 'Support Staff'];

    if (!contactList.isEmpty()) {
        for (Contact con : contactList) {
            for (Case c : Trigger.new) {
                c.ContactId = con.Id;
            }
        }
    }
}

(I know this is a really lame example, but it showcases what we want)

If you have a test that has seeAllData=true and you ran this in a dev org that had at least on Contact with that ContactType set, you'd get 100% coverage for this class.  If you ran this with seeAllData=true in prod but had no Contact records with that contactType set, you'd get 20% coverage for this class.

I would recommend that you look at a class by class base for where your coverage percentages differ in Production vs your Sandbox and then compare the lines that differ and the tests that run.  I would guess that one of your seeAllData tests is expecting data that exists in your Sandbox but does not exist in your Production instance.