I have two apex classes that I am trying to push to production from a sandbox. When I go to validate the change set, it fails on the code coverage part saying that the code coverage is 50% and needs to be 75%. Both of the classes have well above 75% code coverage as one class is at 100% and the other is at 95% from one of the test classes that I wrote within the sandbox. Is there something that I am missing here?
1 Answers
By default all local tests are run during deployment, all custom code your company written (doesn't run tests from installed managed packages. Because they're likely to fail on your required fields and these fails won't stop your deployment).
If you understand what you're doing and are under time pressure to deploy it - you can use the "run specified tests" option & list the test classes. As long as they all have 75% and all triggers being deployed have at least 1% coverage - it'll work. It'll also be quicker. You can do it in the changeset deployment UI as well as in sfdx deploy command options.
But it's a bit of "pro" move, I wouldn't do it unless you have some CI setup and something runs all tests in a sandbox from time to time. You could deploy without realising you've broken functionality you thought is unrelated. It's bit like deploying a new required field or validation rule - "what could possibly go wrong", it's just config, tests don't need to run... boom, headshot.
Proper way to do it would be to refresh sandbox from prod, deploy your code there, run all tests, keep investigating until you bring whole org back to 75%+ and then deploy that.

- 18,088
- 2
- 34
- 46