Sub topic 10
Choosing an Apex test level
This chapter assumes you can run sf project deploy start against an org and read its result, and that you know what an Apex test class is. It covers the four test levels the CLI offers, the defaults, the coverage rules in production, and how to pick a level for a given deployment.
What a test level controls
When you deploy Apex classes or triggers, Salesforce can run Apex tests as part of the deployment. The --test-level flag on sf project deploy start, sf project deploy validate, and related commands decides which tests run. It affects three things:
- How long the deployment takes.
- Whether the deployment can succeed at all, because failed tests or low coverage cause a failure and a rollback.
- Whether you can later quick-deploy the result.
Test levels do not change which components are deployed. They only change which tests run alongside the deploy.
The four levels
| Level | What runs | Typical use |
|---|---|---|
| `NoTestRun` | No tests | Deploys to sandboxes, scratch orgs, and Developer Edition orgs |
| `RunSpecifiedTests` | Only the tests you name with `--tests` | Targeted production deploys of a small change |
| `RunLocalTests` | All tests in the org except those from managed packages | The standard level for production |
| `RunAllTestsInOrg` | Every test, including managed package tests | Rare full-regression checks |
NoTestRun is only valid for deployments to orgs that are not production. Production rejects it when the deployment contains Apex.
Defaults when you omit the flag
If you leave out --test-level, the default depends on the target org and the contents of the deployment:
- Sandbox, scratch org, or Developer Edition:
NoTestRun. - Production, with Apex classes or triggers in the deployment:
RunLocalTests. - Production, with no Apex: no tests are required. Metadata such as page layouts or custom fields can deploy without them.
Don't rely on the production default in scripts. Naming the level explicitly makes the command read the same way in every environment, and it makes a code reviewer's job easier.
sf project deploy start --manifest manifest/package.xml --target-org my-sandbox --test-level RunLocalTestsHere the sandbox deploy runs all local tests even though NoTestRun would be allowed. This is a useful rehearsal before a production release.
Running specified tests
RunSpecifiedTests takes the test names from the --tests flag. You can pass a class name, or a single method as ClassName.methodName. Repeat the flag for each entry.
sf project deploy start --source-dir force-app/main/default/classes/AccountService.cls --target-org my-sandbox --test-level RunSpecifiedTests --tests AccountServiceTest --tests InvoiceServiceTest.testCreateInvoiceThis deploys one class and runs one whole test class plus one test method.
Specified tests carry a stricter coverage rule in production than local tests do, which the next section explains.
Coverage rules in production
Production deployments that include Apex must meet these conditions:
- Every test that runs must pass. One failure fails the whole deployment.
- With
RunLocalTestsorRunAllTestsInOrg, the org-wide Apex coverage must be at least 75 percent. Every trigger in the deployment must also have some coverage. - With
RunSpecifiedTests, each class and trigger in the deployment must individually reach at least 75 percent coverage from the tests you named.
The third rule catches people out. An org may sit at 90 percent overall, yet a specified-test deploy fails because the one new class you are shipping is covered at 60 percent by the tests you listed. Coverage from tests you did not name does not count.
sf project deploy start --manifest manifest/package.xml --target-org production --test-level RunSpecifiedTests --tests AccountServiceTestIf AccountService is the only Apex class in the manifest and AccountServiceTest covers at least 75 percent of it, this deploy meets the rule. Add a second class to the manifest and that class needs its own 75 percent from the named tests.
Choosing a level
Some practical guidance:
- Daily work in sandboxes: use
NoTestRun. The feedback loop matters more than coverage there. - Before a release: rehearse in a full or partial sandbox with
RunLocalTests. Failures found there cost nothing. - Production releases: use
RunLocalTestsunless the suite is too slow and you have well-targeted tests. Then useRunSpecifiedTestsand check per-class coverage first. RunAllTestsInOrg: use it when you want to confirm that managed package tests also pass. These tests are outside your control, so a failure in one may not be fixable by you.
Test levels and quick deploy
A validation is a deployment run with tests but saved nothing. A validated deployment can be quick-deployed later without rerunning tests. That lets you run slow tests ahead of a release window, then promote the result in seconds.
sf project deploy validate --manifest manifest/package.xml --target-org production --test-level RunLocalTests
sf project deploy quick --job-id 0AfXXXXXXXXXXXXXXX --target-org productionReplace the placeholder with the job ID the validate command prints. The level you choose at validation time is the level that counts. The quick deploy runs no tests of its own. Validated deployments are only eligible for quick deploy for 10 days after they succeed. The next chapters in this series on validating and on checking deployment status cover that flow in detail.
Reading test results
When tests run, sf project deploy start prints a test results section after the component results. Look for:
- Failures: the class, method, and message for each failed test.
- Code coverage warnings: classes or triggers below the required percentage.
- Totals: tests run, failures, and elapsed time.
For machine-readable detail, add --json. The Apex test data is nested under the deployment result in the JSON output. To see coverage for each class in the human-readable output, add --coverage-formatters with a format such as text, and --results-dir to choose where the files are written.
sf project deploy start --manifest manifest/package.xml --target-org my-sandbox --test-level RunLocalTests --coverage-formatters text --results-dir test-resultsThis writes coverage output under test-results, which you can read after a failure to find the classes that fell short.
Common mistakes
- Using
NoTestRunagainst production. The deploy fails with a message that tests are required. Switch toRunLocalTestsorRunSpecifiedTests. - Passing
--testswith another level. The--testsflag only applies toRunSpecifiedTests. Setting it withRunLocalTestsis a mistake. Set both flags together so the intent is clear. - Assuming org-wide coverage saves a specified-test deploy. It does not. Each deployed class needs its own 75 percent from the named tests.
- Expecting
RunLocalTeststo be fast. It runs every non-managed test in the org, including ones unrelated to your change. A large org can take an hour or more. - Tests that depend on org data. A test that works in one sandbox can fail in production if it reads existing records without creating its own. Failures like this show up only when tests run, so rehearse with
RunLocalTestsfirst.
Exercises
Exercise
You are deploying one new Apex class, PricingService, to production. The org-wide coverage is 88 percent. You run the deploy with --test-level RunSpecifiedTests --tests PricingServiceTest, and PricingServiceTest covers 70 percent of PricingService. Will the deployment succeed, and why?
Show answer
No. With RunSpecifiedTests in production, each class and trigger in the deployment must individually reach 75 percent coverage from the named tests. At 70 percent, PricingService falls short, and the 88 percent org-wide figure does not count. Add test coverage or name more tests that cover the class.
Exercise
Write a command that deploys the components in manifest/package.xml to the sandbox my-sandbox and runs all local tests.
Show answer
sf project deploy start --manifest manifest/package.xml --target-org my-sandbox --test-level RunLocalTests