Sub topic 2
Choosing a test level
This chapter assumes you can run sf commands, have authorized a sandbox and a production org, and know that Apex tests and code coverage gate production deployments. Here you will learn what each test level runs, how the level affects validation and quick deploy, and how to pick one.
What a test level controls
A test level tells Salesforce which Apex tests to run during a deployment or validation. You set it with the --test-level flag on sf project deploy start and sf project deploy validate. The level changes how long the job takes, which failures can block it, and whether the result can later be used for quick deploy.
If you omit the flag, the default depends on the target org. Sandboxes and scratch orgs default to NoTestRun. Production defaults to NoTestRun when the deployment has no Apex classes or triggers, and to RunLocalTests when it does.
The four levels
| Level | What runs | Typical use |
|---|---|---|
| `NoTestRun` | No tests. | Sandbox and scratch org deployments, or production changes with no Apex. |
| `RunSpecifiedTests` | Only the test classes you name. | Small, targeted production deployments. |
| `RunLocalTests` | All tests in the org, except tests from managed and unlocked packages installed from outside your org. | Standard production release. |
| `RunAllTestsInOrg` | Every test in the org, including packaged tests. | Rare. Used when you must prove the whole org is healthy. |
NoTestRun is not accepted for a production deployment that contains Apex. Salesforce rejects the request, because production needs tests to pass and coverage to be met.
Specified tests and the per-class rule
With RunSpecifiedTests, you list test classes using --tests. The flag is repeated once per class.
sf project deploy validate \
--manifest manifest/package.xml \
--test-level RunSpecifiedTests \
--tests AccountServiceTest \
--target-org prodThis level has a stricter coverage rule. Each Apex class and trigger in the deployment must reach at least 75% coverage from the tests you specify. Under RunLocalTests, the 75% requirement applies to the org overall. This means a class that is covered only by another team's tests will fail a specified-tests run, even though the same class would pass under RunLocalTests.
How the level affects quick deploy
Quick deploy reuses the test results of a validation, so the validation must have run tests that satisfied production. Validations that ran with RunLocalTests, RunAllTestsInOrg, or a passing RunSpecifiedTests are candidates. A validation run with NoTestRun is not eligible for a quick deploy to production when the deployment contains Apex. Later chapters cover the full eligibility rules.
The practical consequence is that you should validate with the same level you would use for the real deployment. Validating at a lighter level and then deploying at a heavier one gives you no time savings, and it hides failures until the release window.
Choosing in practice
Use these criteria in order.
- Does the package contain Apex? If not,
NoTestRunis fine and keeps the job short. - How large is the org's test suite? A large suite under
RunLocalTestscan take an hour or more. That is the reason to validate ahead of time. - Can you name the tests precisely?
RunSpecifiedTestsis faster, but only if each deployed class is covered at 75% by the listed tests. - Is there a compliance or risk reason to run everything? Choose
RunAllTestsInOrg, and expect packaged tests to add failures you do not own.
When unsure, choose RunLocalTests. It is the safest default for production, and it is the level most teams use for validate-then-quick-deploy.
Common errors
- Using
NoTestRunagainst production with Apex. The deployment is rejected before any tests run. - Listing a test class by file name. Pass the class name, such as
AccountServiceTest, notAccountServiceTest.cls. - Expecting
RunLocalTeststo ignore failing tests you did not touch. Any failing local test fails the deployment, even in code you did not change. - Changing the level between validation and deployment. A quick deploy uses the validation's results, so a different level on the real deployment would need a new validation.
Exercise
Your production deployment contains one Apex class, AccountService, and its test AccountServiceTest. AccountService reaches 82% coverage from that test. Another class in the org has low coverage. You validate with RunSpecifiedTests and --tests AccountServiceTest. Will the low-coverage class block the validation, and why?
Show answer
No. With RunSpecifiedTests, the 75% coverage rule applies to each class and trigger in the deployment, not to the whole org. AccountService reaches 82%, so it passes. A class that is not part of the deployment is not checked.