How to deploy Apex without test coverage
March 4th, 2009
A couple of weeks ago I had written some Apex controllers and Visualforce pages for one of our business units. The new code went through release management and I decided to deploy it Friday night (the business unit is located in the UK) before I left for the weekend.
I used Eclipse as usual and my deployment validated successfully and the assets were pushed to production. I logged in and ran the new processes successfully.
While running All Test on one of our dev sandboxes on Monday morning, I realized that I had totally forgotten to write the unit tests for the code I had deployed on Friday. I was shocked that the code had successfully deployed as everyone know that 75% code coverage is required.
I submitted a case to our Premier Support rep which he quickly escalated to Level 3 support. I also decided to post the question to the Salesforce.com message boards. Shortly thereafter, I received the following message from Andrew, a product manager at Salesforce.com:
“Code coverage is measured across all of your apex classes and triggers. Triggers are the only logical blocks that specifically require >0% coverage because we know they are in use. The same can not be said for classes so there is no class-level coverage requirement in place at this time.
When you deploy your code using eclipse the existing tests are run and if the coverage is sufficient your deployment will succeed. I presume you had code deployed already and that the additional (untested) controller code you deployed did not drop your overall coverage below the minimum level. If you do a global run all tests you should still find yourself above the minimum. If that’s not the case please update your case with that information.”
Very interesting. I showed this to our Premier Support rep and he was preplexed to say the least.
Categories: Apex, Salesforce













What’s so perplexing? You need overall code coverage of >75%. If you still have that even with your new untested code, the deploy will succeed. Am I missing something here?
Rich, I think you are right to a certain extent. I went back and looked at a bunch of documents pertaining to unit tests as well as the notes/slides from my DEV-501 class and requirements are not specified clearly. For example, the Apex docs state, “You must have at least 75% of your Apex scripts covered by unit tests to deploy your scripts to production environments. In addition, all triggers should have some test coverage. ” While another wiki post states, “To deploy to a production organization, all Apex classes and triggers need at least 75% code coverage from tests.”
I’ve asked a couple of people on our Salesforce.com team and they interpret that to mean that EACH class/trigger needs to have 75% code coverage. As you rightly state, code coverages is a percentage across all classes/triggers. My argument is that it should be against each class/trigger. You should not be able to deploy to Production with 0% code coverage for a single class/trigger.
Interesting…. I just created a new Apex Class in Force.com and used the Test Class template. It created a new Class with the following comments, “All Apex classes are required to have at least 75% code coverage in order to be deployed to a production organization.” This is why this topic is confusing. Contradicting statements from Salesforce.com
To me, test coverage is one of the more complicated/difficult parts of Salesforce Development. Reading through these comments it looks like you’ve got it figured out for the most part as far as why this might have worked, but one thing that took me a while to realize is that if you have any test coverage in the system (i.e. test coverage for a trigger/class on the Account object) then it will more than likely cover at least a portion of any other Account triggers you may deploy…it just depends on which DML statements (insert, update) the coverage is running and what you have your trigger set to fire based on (before insert, after insert, before update). I like to use @isTest classes (one per object) to write my test coverage – that way if I add another trigger on the object I can easily go in and update the coverage to cover more of my code. I know it’s sometimes impractical, but I ALWAYS shoot for 100% coverage – you won’t regret it one bit when you run across some code that you can’t seem to get above 74% (there are some apps we’ve downloaded/installed from appexchange that appear to not have hardly any testcoverage. Salesforce even has apps they’ve released that require you to bypass the testcoverage when installing – we’ve run into issues after deployment because of apps like this…
i am a learner in salesforce now and need help with the use of custom setting for custom objest. help me with that please as i have read ur various posts and they are really very helpful
[...] coverage. Tons and tons of stuff has been written about test coverge, including inventive ways to get around it. I’ll save you some digging, and provide you with a class that exercises this trigger. You may [...]
Duh!!!! No surprise!
you can also write a 40 lines code and then a 1000 lines of useless code and include useless code in test… it will work too… tests shmests…
Hi,
Is there any method of deploying in production instance without executing test methods, Yesterday When I deployed using Eclipse, it deployed to my production instance in minutes and did not executed any test class ( Usually it takes 2 hrs to run all test class).