Recommended
Why RunSpecifiedTests fails on a class RunLocalTests passes (opens in a new tab)
You will learn how RunLocalTests and RunSpecifiedTests apply different per-class coverage rules, why a specified run can fail at 0% coverage, and where the coverage warning appears in Metadata API results.
RunLocalTests
- Runs all local tests
- Overall coverage of at least 75%
- Coverage counted from all tests that ran
RunSpecifiedTests
- Runs only the named tests
- Each class at least 75% individually
- Coverage counted from named tests only
Salesforce production deployments are usually validated at one of two test levels, RunLocalTests and RunSpecifiedTests. RunLocalTests runs every test in the org except those from installed managed and unlocked packages, and it is the default when the package contains Apex classes or triggers. RunSpecifiedTests runs only the tests named in the runTests option.
The two levels count coverage differently. RunLocalTests requires overall org coverage of at least 75 percent, so a weak class can pass if the rest of the org is well covered. RunSpecifiedTests requires each Apex class and trigger in the package to reach 75 percent individually, and only the named tests count toward that figure. A class covered by none of the named tests therefore fails the deployment even when a full local run would have passed.
The article gives a hypothetical package of 13 components with three Apex classes, where two named tests cover two of them and the third sits at 0 percent. Two fixes are offered: add a test that covers the class, or remove the class from the package if it has not changed. Coverage failures are reported under details.runTestResult.codeCoverageWarnings in the checkDeployStatus() result, which appears only when includeDetails is true, so tooling that prints just the error lists can make such a failure look unexplained.
The author suggests RunLocalTests when the simplest and safest run matters more than time, or when it is unclear which tests cover which classes. RunSpecifiedTests suits teams that know the covering tests for every class and want a shorter run. The piece also notes that RunRelevantTests is in beta and should be checked against current documentation, and that NoTestRun applies only to development environments, not production. The Salesforce CLI exposes these choices through –test-level and –tests. The article closes with a note on the author’s own tool, Diffbridge.
Read the original at dev.to (opens in a new tab)