Two test levels cover almost every production deployment: RunLocalTests and RunSpecifiedTests. They look interchangeable, but they apply different coverage rules, and the difference explains a failure many people hit: a deployment that passes with RunLocalTests and fails with RunSpecifiedTests on a class showing 0% coverage. This post sets the two side by side, using Salesforce's Metadata API documentation.
What each one runs
RunLocalTestsruns all tests in your org, except tests that come from installed managed and unlocked packages. It is also the default for a production deployment whose package contains Apex classes or triggers.RunSpecifiedTestsruns only the tests you name in therunTestsoption, and nothing else.
Neither is chosen for you by what changed. With the default behaviour, what decides the production test run is what is in the package.
The coverage rules differ
This is the part that matters.
RunLocalTests |
RunSpecifiedTests |
|
|---|---|---|
| Which tests run | All local tests | Only the ones you name |
| Coverage required | Overall coverage of at least 75%, and Apex triggers have some coverage | Each Apex class and trigger in the deployment package covered at least 75% individually |
| What the coverage is counted from | All tests that ran | Only the tests you named |
With RunLocalTests, a well-covered org can carry a weak class: only the overall figure has to reach 75%. With RunSpecifiedTests, Salesforce computes coverage for each class and trigger in the package, and it is not the same as the overall percentage.
Why a specified run fails with 0% coverage
Here is a made-up example. Take a package with 13 components, including 3 Apex classes, deployed with RunSpecifiedTests and two named tests that cover two of those classes. The third class is covered by neither test. In this run its coverage is 0%, which is below the per-class 75%, so the deployment fails. The full local run would have passed, because the org's overall coverage was fine and the other tests would have covered the third class.
Two fixes follow from the rule:
- Add the test that covers that class to the list.
- Take the class out of the package if it hasn't changed. A class that isn't in the package needs no coverage in that run.
Where the failure shows up
A coverage failure isn't reported with component errors or failing tests. In the Metadata API it appears under details.runTestResult.codeCoverageWarnings in the checkDeployStatus() result, which you only get when you call it with includeDetails=true. If your tooling prints just the error lists, a coverage problem can look like a deployment that failed for no reason.
Which to pick
- Use
RunLocalTestswhen you want the safest, simplest run and the time isn't a problem, or when you can't say which tests cover which classes. - Use
RunSpecifiedTestswhen you know the tests for every class in the package and want a much shorter run. Check each class's coverage first. - Remember
RunRelevantTests. Salesforce marks it as beta. It chooses tests from the deployment payload and its dependencies, so read its current documentation before relying on it. - Remember
NoTestRun. It applies only to development environments such as sandboxes, Developer Edition and trial orgs, not to production.
The Salesforce CLI takes the same choices as --test-level with --tests for the named classes.
How Diffbridge handles it
Diffbridge's Smart test level uses the same rule for you. If nothing in the package adds, changes or deletes Apex, it runs no tests. If Apex changed, it runs the related tests and shows the plan with reasons first. In production, when a changed class has no related test, it falls back to all local tests, which sidesteps the per-class check that causes the failure above. The manual page on tests has the details.
I build Diffbridge, a Salesforce deployment tool. Nothing above needs it. If you spot a mistake in the test-level details, tell me and I'll fix it.