Sub topic 2
Choosing a test level
This chapter assumes you can read a package.xml manifest, authorize an org with the Salesforce CLI (sf), and write a basic Apex test class. It also assumes you know that production deployments that include Apex must pass tests. The chapter explains the four test levels, what each one runs, and how to pick one for a validation.
Why the test level matters
A test level is a deployment setting that tells the target org which Apex tests to run. The setting decides three things:
- How long the deployment takes. Test execution is usually the slowest step.
- Which coverage rules the org checks.
- Whether the validation can be promoted later with quick deploy.
The same manifest can pass at one level and fail at another. A run that executes only a few tests can look healthy while the org as a whole falls below the coverage threshold. A run that executes every test can fail because of an unrelated test that has nothing to do with your change.
The four test levels
The Metadata API defines four levels. The CLI passes them through with the --test-level flag.
| Test level | What runs | Typical use |
|---|---|---|
| `NoTestRun` | No Apex tests | Sandboxes, scratch orgs, and Developer Edition orgs |
| `RunSpecifiedTests` | Only the test classes you name | Small, targeted production changes |
| `RunLocalTests` | All tests in the org except tests from installed managed packages | The standard production validation |
| `RunAllTestsInOrg` | Every test in the org, including managed package tests | Full regression checks, rarely a release default |
NoTestRun
NoTestRun runs no Apex tests. It applies only to development environments such as sandboxes, scratch orgs, and Developer Edition orgs. It is the default there, which is why deployments to a sandbox are fast and do not exercise your tests unless you ask them to.
Production does not accept NoTestRun for a package that contains Apex classes or triggers. A production deployment that contains no Apex, such as a page layout or a custom field, runs no tests by default. Such a deployment has no test run for quick deploy to skip, so it gains nothing from the technique this tutorial covers.
RunSpecifiedTests
RunSpecifiedTests runs only the test classes you list. You pass the names with the --tests flag, and you can repeat the flag. Always set --test-level RunSpecifiedTests together with --tests, so the intent is explicit.
The coverage rule for this level differs from the other levels. Each Apex class and trigger in the deployment package must have at least 75% coverage on its own, and the tests you named must produce that coverage. An org-wide average does not rescue a weakly covered class, and tests you did not name do not count.
Because only the named tests run, the run is faster. The cost is that you carry the responsibility for choosing the right tests. If you leave out a test class that exercises a changed class, you get a coverage failure. If you leave out a test that would have caught a regression in other code, the validation passes and the regression ships.
RunLocalTests
RunLocalTests runs every test in the org except tests that come from installed managed packages. It is the default for production when the package contains Apex classes or triggers. Most teams use it for release validations, because it checks your change against all of your org's own tests without running code you do not own.
The coverage rule for this level is org-wide. The Apex in the org must reach at least 75% overall coverage, and every trigger needs some coverage. Chapter 3 covers the details of how the coverage figure is calculated. For now, remember that a change with perfect tests can still fail if the rest of the org has drifted below the threshold.
RunAllTestsInOrg
RunAllTestsInOrg runs every test, including tests from installed managed packages. It takes the longest. It also fails more often for reasons outside your control, because a managed package test can depend on org data or configuration that you cannot change. Use it when you want a full regression check, not as a routine release gate.
Choosing a level
Several factors push you toward one level or another. The diagram shows one reasonable decision path.
Use these criteria when the path leaves you undecided:
- Risk. A change to shared code, such as a trigger handler or a utility class, can break tests you would not think to name.
RunLocalTestscatches that.RunSpecifiedTestsdoes not. - Time. Orgs with thousands of tests can spend an hour or more in
RunLocalTests. If you validate ahead of the release window and quick deploy inside it, the duration matters less, because the wait happens outside the window. - Coverage margin. If the classes you are deploying already have strong coverage from their own test classes,
RunSpecifiedTestsis enough. If you are unsure, runRunLocalTestsand read the coverage output. - Test health. If some existing tests fail for reasons unrelated to your change,
RunLocalTestsfails the validation. You must fix those tests or choose a different approach. - Quick deploy. A validation can be promoted only if tests ran and passed in the target org. A later chapter lists the full eligibility rules, but the test level you pick now decides whether the option exists.
Running validations at each level
The following manifest names an Apex class and its test class. It is the same file the later chapters reuse.
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>AccountService</members>
<members>AccountServiceTest</members>
<name>ApexClass</name>
</types>
<version>60.0</version>
</Package>To validate with only the tests you name, set the level and list the classes:
sf project deploy validate \
--manifest manifest/package.xml \
--test-level RunSpecifiedTests \
--tests AccountServiceTest \
--target-org prodTo name several test classes, repeat the flag:
sf project deploy validate \
--manifest manifest/package.xml \
--test-level RunSpecifiedTests \
--tests AccountServiceTest \
--tests ContactServiceTest \
--target-org prodTo run all local tests, which is the usual production choice, change only the level:
sf project deploy validate \
--manifest manifest/package.xml \
--test-level RunLocalTests \
--target-org prodThe validate command accepts RunSpecifiedTests, RunLocalTests, and RunAllTestsInOrg. The next chapters cover the command itself and its output in detail. Here the point is that the only change between these runs is the test flags.
The same flags work on sf project deploy start, the command that does save changes. The commands differ only in whether the org keeps the result.
Default behavior when you omit the flag
If you omit --test-level, the org chooses for you:
- In a sandbox or other development org, no tests run.
- In production, if the package contains Apex classes or triggers,
RunLocalTestsruns. - In production, if the package has no Apex, no tests run.
Relying on the default is fine for a quick check, but it hides the decision. For release work, state the level in the command or in your pipeline configuration. A script that says --test-level RunLocalTests documents the intent and behaves the same if the default changes.
Common errors
These are the failures you will meet most often when the test level and the package disagree.
| Message or symptom | Likely cause | Fix |
|---|---|---|
| `Test coverage of selected Apex Class is 60%, at least 75% required` | Under `RunSpecifiedTests`, the named tests do not cover a class in the package well enough | Add the missing test class to `--tests`, or improve the tests |
| `Average test coverage across all Apex Classes and Triggers is 70%, at least 75% required` | Under `RunLocalTests`, the org-wide figure is below the threshold | Add tests for poorly covered code, including code outside your change |
| `Test coverage of selected Apex Trigger is 0%, at least 1% required` | A trigger in the package has no test that fires it | Add a test that performs the DML the trigger responds to |
| A test fails with an error unrelated to your change | An existing test is broken, or a managed package test depends on org data | Fix the test, or move from `RunAllTestsInOrg` to `RunLocalTests` |
| The validation passes but a quick deploy is not offered | Tests did not run, or a rule in the next chapters is not met | Re-run the validation at a level that executes tests in the target org |
A frequent mistake is to name only the test class that matches the class you edited. If you changed a trigger handler, the tests for the trigger, the handler, and any class that calls the handler all matter. When in doubt, move up to RunLocalTests.
A practical habit
For a release, run a first validation at RunLocalTests against production well before the window. If it passes, you have both the safest result and a job ID to promote. Use RunSpecifiedTests for small, urgent fixes where the full run would cost more time than the risk justifies, and write the test class names into the ticket so the choice is traceable.
Exercise
A teammate validates a change to a shared trigger handler against production with --test-level RunSpecifiedTests --tests TriggerHandlerTest. The validation passes. Why might you still ask them to re-run it with RunLocalTests, and what coverage rule applies to each run?
Show answer
Under RunSpecifiedTests, only TriggerHandlerTest runs, so tests for other classes that call the handler never execute, and a regression in them would not be detected. The coverage rule is per component: each class and trigger in the package needs at least 75% coverage from the named tests. Under RunLocalTests, all non-managed-package tests in the org run and the rule is org-wide: at least 75% overall coverage, with every trigger covered. Because the handler is shared, RunLocalTests gives broader protection.
Exercise
You deploy a package to a sandbox without setting --test-level, and then to production with the same package, which contains one Apex class. What test behavior do you get in each org by default?
Show answer
In the sandbox, no tests run, because NoTestRun is the default for development environments. In production, RunLocalTests runs by default because the package contains Apex. Setting --test-level explicitly removes the difference in surprise and documents the intent.
Exercise
Write the command that validates manifest/package.xml against the org alias prod, running only AccountServiceTest and ContactServiceTest.
Show answer
sf project deploy validate --manifest manifest/package.xml --test-level RunSpecifiedTests --tests AccountServiceTest --tests ContactServiceTest --target-org prod