+ Start a Discussion
Colin LoretzColin Loretz 

Apex tests pass but fail on deploy

Hi there,

I'm having some issues deploying a large amount of Apex code to my production org. I have spent the last week writing Apex test methods and the average coverage is well above 75% for all the code. I run the Apex Code Test Runner in Eclipse and it mentions that most of my triggers at are 100%, a few just below.

I go to the project > Force.com > Deploy to Server

Once I enter the destination information, select the actions to act on, and hit "validate deployment", it returns saying that it has failed.

The reason for the failure being that the average coverage for my code is only 27%. I know this to be false so I'm not sure why the tests work (with 0 failures) when developing in Eclipse or even in the Sandbox UI but not when deploying to a production.

Do tests get run differently in a production instance?

Message Edited by Colin Loretz on 07-11-2008 01:21 AM

Message Edited by Colin Loretz on 07-11-2008 01:21 AM

These types of errors are almost always the result of polluting your tests with data that are not created within your test methods, but are already in your Salesforce organization.  When this happens, you get different results (and coverage numbers) executing the same test in different environments, because the data set determines which branches of code are executed at runtime.

To resolve this issue and make your tests work everywhere, look at every query you execute in your test methods and actual code, and make sure that within your test methods you create a sufficiently rich data set to cover all important scenarios.

Data changes performed within a test method execution are not committed to the database, so you don't have to worry about altering your production data this way.

Colin LoretzColin Loretz

Thank you for the quick response. I will clean up the tests and make sure they are only using data created by the tests.