Sub topic 11
Setting test levels with sf project deploy
This chapter assumes you know basic Apex and the @IsTest annotation. It also assumes you can tell sandboxes from production and have run sf commands before. You have already seen what the four test levels run. Here you will learn how to choose one from the Salesforce CLI, what each flag does, and how to confirm which tests ran.
The flags that control tests
The sf project deploy start command takes the test level from one flag and the test class names from another.
| Flag | Purpose | Notes |
|---|---|---|
| `--test-level` | Chooses the level | One of `NoTestRun`, `RunSpecifiedTests`, `RunLocalTests`, `RunAllTestsInOrg` |
| `--tests` | Names the test classes to run | Used with `RunSpecifiedTests`. Repeat the flag or list several names |
| `--target-org` (`-o`) | The org to deploy to | Its type decides the default level |
| `--source-dir` (`-d`) | The source to deploy | Use `--metadata` or `--manifest` instead if you prefer |
| `--wait` (`-w`) | Minutes to wait for the result | Test runs are slow, so allow enough time |
| `--dry-run` | Runs the deployment checks without saving changes | Tests run according to the test level |
| `--verbose` | Prints per-test and per-class detail | Useful for reading coverage |
Level names are case-sensitive in practice. Copy them exactly as written above.
The four levels as commands
NoTestRun
sf project deploy start --source-dir force-app --target-org my-sandbox --test-level NoTestRunNo Apex tests run. This is the fastest option. Production rejects it when the deployment contains Apex classes or triggers. Use it for sandboxes and scratch orgs.
RunSpecifiedTests
sf project deploy start --source-dir force-app --target-org my-prod --test-level RunSpecifiedTests --tests AccountServiceTestOnly the named classes run. To name more than one, repeat the flag:
sf project deploy start --source-dir force-app --target-org my-prod --test-level RunSpecifiedTests --tests AccountServiceTest --tests ContactServiceTestThe names are test class names, not the classes under test. Naming AccountService here would not run its tests. Name AccountServiceTest.
RunLocalTests
sf project deploy start --source-dir force-app --target-org my-prod --test-level RunLocalTestsEvery test in the org runs except tests that come from installed managed packages. This is the default for a production deployment that contains Apex.
RunAllTestsInOrg
sf project deploy start --source-dir force-app --target-org my-prod --test-level RunAllTestsInOrgThis runs everything, including managed package tests. It is the slowest level. Failures in package tests that you do not control can block your deployment, so use it deliberately.
What happens when you set no level
If you omit --test-level, the target org decides:
- A sandbox, scratch org or Developer Edition org uses
NoTestRun. - A production org uses
NoTestRunwhen the deployment has no Apex classes or triggers. - A production org uses
RunLocalTestswhen the deployment includes Apex.
Relying on this default is a common source of surprise. A pipeline that deploys to a sandbox with no flag runs no tests. The same command pointed at production suddenly runs the entire local suite and takes much longer. Setting the flag explicitly in every pipeline command keeps the behavior the same in both places.
Pairing --tests with RunSpecifiedTests
The --tests flag only has meaning with RunSpecifiedTests. Pass it with one of the other levels and you are asking for something the level does not do, so drop it. Pass RunSpecifiedTests with no --tests and there is nothing to run, so the deployment fails.
Coverage is where this level catches people. Each class and trigger in the deployment must reach 75% coverage from the tests you named, one by one. The org-wide average does not count. If you deploy AccountService and ContactService but name only AccountServiceTest, ContactService has no coverage from your run and the deployment fails, even if other tests in the org cover it well.
Use a small deployment and its matching test classes together:
sf project deploy start \
--source-dir force-app/main/default/classes/AccountService.cls \
--source-dir force-app/main/default/classes/AccountServiceTest.cls \
--target-org my-prod \
--test-level RunSpecifiedTests \
--tests AccountServiceTest \
--wait 30Include the test class in the deployment if it does not yet exist in the target org. The tests you name must be present in the org when the run starts, either already there or arriving in the same deployment.
Validating, then quick deploying
A production release often has a long test run. You can run the tests ahead of time and save the result.
sf project deploy validate --source-dir force-app --target-org my-prod --test-level RunLocalTests
# The command prints a job ID. Use it to deploy without re-running the tests:
sf project deploy quick --job-id <validated-deploy-job-id> --target-org my-prodvalidate runs the full deployment checks, including tests, and saves nothing. quick then deploys the same validated change and skips the tests. Salesforce allows this only inside a limited window after the validation succeeds. A new validation is needed once the window passes. A quick deploy also requires that the validation ran tests at a level production accepts, so validate with RunLocalTests or higher, or with RunSpecifiedTests when it met the coverage rules.
If you lose the job ID, sf project deploy report lists it for a recent deployment, or --use-most-recent on quick selects the latest validation.
Reading the results
Tests running is not the same as tests passing. Check the outcome in the output, or ask for more detail:
sf project deploy start \
--source-dir force-app \
--target-org my-prod \
--test-level RunLocalTests \
--wait 60 \
--verbose \
--results-dir test-results \
--coverage-formatters json-summary--verbose prints the tests that ran. --results-dir writes result files that a pipeline can archive. --coverage-formatters chooses the coverage report format, which you can feed to a coverage tool or a build check. If you start a deployment without waiting, you can fetch the result later:
sf project deploy report --job-id <deploy-job-id>Common mistakes
- Using
NoTestRunagainst production with Apex. The deployment is rejected. Switch toRunLocalTestsorRunSpecifiedTests. - Naming the class under test instead of the test class.
--tests AccountServicefinds no test to run. UseAccountServiceTest. - Forgetting
--testswithRunSpecifiedTests. No tests are selected, and the deployment cannot succeed. - Naming too few test classes. Each deployed class needs 75% coverage from the named tests alone.
- Using a short
--wait. The CLI stops waiting while the deployment keeps going in the org. Usesf project deploy reportto pick it up again. - Depending on defaults. A command with no
--test-levelbehaves differently on a sandbox and on production.
Exercise
A pipeline step runs sf project deploy start --source-dir force-app --target-org my-prod with no other flags. The package contains two Apex classes and their tests. Which test level applies, and how would you change the command to run only OrderServiceTest, assuming that class covers every deployed class to at least 75%?
Show answer
Production with Apex in the package defaults to RunLocalTests, so the whole local suite runs. To run only one class, add --test-level RunSpecifiedTests --tests OrderServiceTest. Each deployed class must reach 75% coverage from that one test class. If either deployed class falls below that, the deployment fails.
Exercise
You validated a production release with sf project deploy validate and got a job ID. What command deploys it without re-running tests, and what condition can make it fail?
Show answer
sf project deploy quick --job-id <job-id> --target-org my-prod. It fails if the validation is outside the allowed window, so you must validate again.