Sub topic 9
Which level applies when you set none
This chapter assumes you know what the four test levels run and can use sf project deploy start. Here you learn what Salesforce chooses when you leave out --test-level.
The default depends on the target and the contents
Salesforce picks the default from two facts about the deployment:
- Is the target org a production org or not?
- Does the package contain Apex classes or triggers?
The org type alone does not decide it. A production deployment without Apex behaves differently from one with Apex.
| Target org | Package contains Apex classes or triggers | Default level |
|---|---|---|
| Sandbox, Developer Edition, trial or scratch org | Yes or no | NoTestRun |
| Production | Yes | RunLocalTests |
| Production | No | NoTestRun |
Non-production orgs: nothing runs
Deploying to a sandbox without a test flag runs no Apex tests. This keeps development deployments fast. The following command and the explicit NoTestRun form do the same thing:
sf project deploy start --source-dir force-app --target-org my-sandbox
sf project deploy start --source-dir force-app --target-org my-sandbox --test-level NoTestRunBecause no tests run, a sandbox deployment can succeed with failing tests or with coverage under 75%. The problem shows up later, when the same code is deployed to production.
Production with Apex: RunLocalTests
If the package includes at least one Apex class or trigger, a production deployment runs RunLocalTests automatically. That means every test in your org except tests from installed managed packages. The 75% coverage requirement then applies to the deployment, as the next chapter explains.
# These two commands behave the same when the package contains Apex
sf project deploy start --source-dir force-app --target-org my-prod
sf project deploy start --source-dir force-app --target-org my-prod --test-level RunLocalTestsSetting the level explicitly costs nothing, and it makes the intent visible in a pipeline file.
Production without Apex: no tests
A production deployment that contains only non-Apex metadata, such as a custom field or a page layout, runs no tests by default. Salesforce has no Apex in the package to verify.
The edge case is a package that touches Apex indirectly. Adding a new class or trigger to the package changes the default from no tests to RunLocalTests. A change that looked "metadata only" in review can therefore start running the whole test suite.
Common errors
- Assuming a sandbox deploy proves the tests pass. The sandbox default runs none, so a green sandbox deployment says nothing about test health.
- Forcing NoTestRun on production. Production does not accept NoTestRun for a package that includes Apex. The deployment fails instead of skipping tests.
- Being surprised by a long production deployment. RunLocalTests runs your whole org's test suite. A large org can spend a long time on tests alone.
- Relying on the default in CI. If a CI job targets a sandbox and a release job targets production, the same command runs different tests. Set
--test-levelin both.
Exercise
Your package adds one custom field and one new Apex trigger. You run sf project deploy start --source-dir force-app --target-org my-prod with no other flags. Which test level applies, and what changes if you remove the trigger from the package?
Show answer
RunLocalTests applies, because the target is production and the package contains an Apex trigger. If you remove the trigger, the package has no Apex, so the default becomes NoTestRun and no tests run.