Sub topic 10
Choosing an Apex test level
This chapter assumes you can run sf project deploy start against an authorized org and know what an Apex test class is. It covers how to control which Apex tests run during a deployment.
What a test level does
A deployment can run Apex tests before it commits changes. If a test fails, the whole deployment fails and nothing is saved. You choose how many tests run with the --test-level flag.
The four levels
| Level | Tests that run | Typical use |
|---|---|---|
| `NoTestRun` | None | Sandboxes and scratch orgs, or changes with no Apex |
| `RunSpecifiedTests` | Only the tests you name | Small, targeted production deploys |
| `RunLocalTests` | All tests in the org except those from managed packages | Most production deploys |
| `RunAllTestsInOrg` | Every test, including managed package tests | Rare full checks |
If you omit --test-level, the default depends on the target org and the package contents. Deploying Apex classes or triggers to production runs local tests by default. Deploying to a sandbox or scratch org runs none.
Running local tests
This command deploys the manifest components and runs all local tests:
sf project deploy start --manifest manifest/package.xml --target-org my-sandbox --test-level RunLocalTestsManaged package tests are skipped. That makes this level faster and more predictable than RunAllTestsInOrg, because you don't control package code.
Running specified tests
Name the tests with --tests. Repeat the flag for each test class:
sf project deploy start --manifest manifest/package.xml --target-org production --test-level RunSpecifiedTests --tests AccountServiceTest --tests InvoiceTriggerTestThis level is quick, but it has a stricter coverage rule. In production, every Apex class and trigger in the deployment must have at least 75% coverage from the tests you list. If one of your changed classes is only covered by a test you didn't name, the deploy fails.
Coverage rules in production
Production enforces code coverage.
- With
RunLocalTests, the org as a whole must have at least 75% Apex coverage. - With
RunSpecifiedTests, each deployed class and trigger must reach 75% on its own. - All tests that run must pass.
Sandboxes don't enforce coverage on deploy.
Common errors
- Specified tests don't cover a changed class. The deploy fails on the 75% per-class rule. Add the missing test class to
--tests, or switch toRunLocalTests. - Passing
--testswith another level. The--testsflag belongs withRunSpecifiedTests. Using it with a different level causes a validation error. - Slow deployments from
RunAllTestsInOrg. Managed package tests add time and can fail for reasons you can't fix. PreferRunLocalTests. - Skipping tests in a sandbox, then failing in production. A sandbox deploy with
NoTestRunnever exposes broken tests. Validate against production with tests before release day. The next chapter covers validating and quick-deploying.
Exercise
You deploy one changed Apex class, PricingService, to production with --test-level RunSpecifiedTests --tests AccountServiceTest. The deploy fails on coverage, although AccountServiceTest passes. What is the likely cause, and what are two ways to fix it?
Show answer
With RunSpecifiedTests, each deployed class needs at least 75% coverage from the listed tests, and AccountServiceTest probably doesn't exercise PricingService. Fix it by adding the test class that covers PricingService (for example --tests PricingServiceTest), or by switching to --test-level RunLocalTests.