Answer – if your test suite is not self contained i.e. it depends on specific data in Sandbox.
I got a call from one of our customers today about not being able to deploy into their production org due to inadequate test coverage even though they were getting 100% coverage in their development sandbox. Our support tracked it down to hard coded test methods. If your test methods depend on specific data in an org and you move such a test method to a different org, it will fail.
Here is an example where the record Id is hardcoded…..
static testMethod void myTestHardcodedIds(){ // INCORRECT - By hardcoding this Account Id in the test method, the test method // will fail in every other org where this code is deployed because the hardcoded // Account Id won't exist there. Account testAccount = [select id,name from Account where Id='001300000040lMM']; testAccount.billingState='CA'; update testAccount;
// Verify that the billingState field was updated in the database. Account updatedAccount = [SELECT billingState FROM Account WHERE Id = :testAccount.Id]; System.assertEquals('CA', updatedAccount.billingState);
The right way to write test methods is to make them self contained i.e. not depend on data in a particular org. I have taken the above example from this great article on testing. I encourage you to go through that to find out how to write the above test method correctly as well as other best practices.
