I have six Apex classes [No triggers] that I am trying to deploy. The code coverage for each class is different: 68%, 73%, 75%, 89%, 89%, and 72%.

So theoretically the average should be over 77% code coverage.

But when I validate the Inbound Change set it tells me that Average test coverage across all Apex Classes and Triggers is 22%, at least 75% test coverage is required.

Can someone explain to me how that is possible?

  • Is the code coverage error the only problem you have with the change sets, or are there other validation failures? If a test class fails to deploy, the code coverage will be affected by the lack of test code. Oct 9 '13 at 22:01
  • The code coverage is the only problem. Oct 9 '13 at 22:12
  • Ok, i have a successful validation; i just needed to deploy my test codes too. Can someone please confirm that all the existing organization objects will only be updated, if needed, i.e. if they already exist nothing will happen? Oct 9 '13 at 22:17

The code coverage is calculated across all classes and triggers in the target org, not just what is in your change set. There must have been a change in your org that is causing the poor coverage. Hard to say exactly what it is, though. Try running all tests in the sandbox and see what the org code coverage is.

  • Could you explain what you mean by running all the tests? Currently i have 3 tests that covers all 6 Apex codes, i run them all together using Apex Test Execution. Then when i go and look at each code coverage u get the percentages above. Oct 9 '13 at 21:08
  • Many times when I find production orgs that have low code coverage it is due to new validation rules that have been added to common test objects, like Contacts or Accounts, that cause the existing test code in the production org to fail, where as it passed prior to the validation rules Oct 9 '13 at 21:12
  • So what should i do in this situation? how can i check what you guys are mentioning? Oct 9 '13 at 21:27
  • Ok so i ran organization code coverage in the sandbox and it says:Code Coverage: 77% Your overall code coverage is currently 77%. To deploy code to production, you must have at least 75%. SO i was right my overall code coverage is 77%. Oct 9 '13 at 21:46
  • 1
    And in my organization it says:Code Coverage: 81% Your overall code coverage is currently 81%. To deploy code to production, you must have at least 75%. So i dont really understand what i should check? Oct 9 '13 at 21:48

You need to remember also that you are running the tests against a different data set. If you aren't creating all you own test data in your test methods and not stating @isTest(seealldata=false) then you will get different results.

You'll need to check the debug logs to see if an queries are returning data and that the data returned covers all the cases for your code functions.

  • Well it doesnt cover all the code, i dont have 100% coverage, but most codes are above 75%. Oct 9 '13 at 21:32

I don't think the overall is an average, it's the actual % covered of lines of code. So if your 68% class is a class with 1000 lines of code and the others are all around 50, chances are you'll fall short.

Given that when you deploy you're seeing 22% as the number, that would indicate that a large number of tests are failing. These could be in production already, so the first step would be to run tests in the target org to see what the existing coverage is. If it's low you'll need to work out why and resolve the issue, it's reasonably easy to change configuration that breaks tests.

If the target org has good coverage then chances are your tests are not running the same way they do in the development org. Reasons for this could be:

  • Optional features not enabled (record types on a specific object for instance)
  • Tests are dependant on existing data
  • Tests are using hard coded IDs (DANGER!)
  • There are workflow / validation rules in production not in the development org
  • Well org overall coverage is 81%. I am deploying all the objects, so as i understood, they should update the existing objects? I am adding data to each test and i have @isTest(seealldata=false) in each test code. I dont have any hard coded ID's, i am not sure how that is even possible, i have many users, contacts, contracts, etc. What do you mean by workflow / validation rules? Oct 9 '13 at 22:00
  • Validation rules determine what values are valid for fields, workflows are like triggers but built declaratively. How are you deploying? You should be able to see which tests are failing and where.
    – Matt Lacey
    Oct 9 '13 at 22:03
  • Non of them fail they all succeed, i get a green check mark in all the tests. Oct 9 '13 at 22:13
  • That's interesting.... Was your development environment in sync with the target org before development started (i.e. was it a full refresh?). It's making me think that something you're deploying is breaking other tests... a trigger or something perhaps?
    – Matt Lacey
    Oct 9 '13 at 22:22
  • Well its working now, i did not no i needed to deploy the test codes too. Oct 9 '13 at 22:41

The 75% refers to ALL your Apex classes and Triggers, not just the small subset you are trying to deploy.

"When deploying to a production organization, every unit test in your organization namespace is executed."


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.