Sub topic 9
Which level applies when you set none
This chapter assumes you can write an @IsTest class, know the difference between a sandbox and production, and have run sf project deploy start before. It covers what happens when you leave out the test level. Teams often depend on this default without realizing it.
The short answer
If you set no test level, Salesforce picks one from two facts: the type of the target org and the contents of the deployment package.
| Target org | Package contents | Test level applied |
|---|---|---|
| Sandbox, scratch org, Developer Edition, trial | Anything | NoTestRun |
| Production | Contains at least one Apex class or trigger | RunLocalTests |
| Production | No Apex classes or triggers | NoTestRun |
The same command can therefore run no tests against a sandbox and your whole local test suite against production. Nothing in the command line shows this.
Why the default depends on the org type
Salesforce treats production as the org that holds live data and serves real users. It requires proof that Apex code works before that code lands there. A sandbox is a place to experiment, so it doesn't make you pay for a full test run on every deployment.
Apex tests run on the target org, so a full run on a large org can take minutes or hours. The default pushes that cost to the one place where it is required.
For the purposes of this rule, "not production" covers more than sandboxes. Scratch orgs, Developer Edition orgs and trial orgs all fall on the NoTestRun side.
Walking through the three cases
Case 1: Deploying to a sandbox with no flag
sf project deploy start --source-dir force-app --target-org my-sandboxNo --test-level is given and the target is a sandbox, so the level is NoTestRun. No tests run, even if force-app holds dozens of classes and triggers. The deployment succeeds or fails on metadata validity alone, such as compile errors and missing dependencies.
This is fast, and it has a trap. A sandbox deployment that succeeds tells you the code compiles. It does not tell you the tests pass or that you meet coverage.
Case 2: Deploying Apex to production with no flag
sf project deploy start --source-dir force-app --target-org my-prodThe target is production and the package contains Apex, so the level is RunLocalTests. Every test in the org runs, except tests that come from installed managed packages. The deployment also has to meet the coverage rules for that level, which the next chapter covers in detail.
This is the same as passing the level yourself:
sf project deploy start --source-dir force-app --target-org my-prod --test-level RunLocalTestsWriting the flag out is still better practice. It documents your intent in the pipeline file, and it protects you if someone later changes the target alias.
Case 3: Deploying non-Apex metadata to production
Suppose your package contains only a custom field, a page layout and a permission set.
sf project deploy start --source-dir force-app/main/default/layouts --target-org my-prodThere are no Apex classes or triggers, so the level is NoTestRun, even though the target is production. Production doesn't run tests for the sake of running them. The rule is tied to Apex in the package.
The edge case to remember: the decision looks at what is in the deployment, not at what already exists in the org. A layout-only deployment to an org with 500 Apex classes still runs no tests.
The decision as a flow
What the CLI sends
When you omit --test-level, the CLI sends no test level in the request. The org applies its default. That matters for two reasons.
- The default can differ between environments even though the command text is identical. A script that works against a sandbox may fail against production because production applies RunLocalTests.
- The CLI output is the only place you learn what ran. After a deployment, look for the test results section. If it is missing, no tests ran.
Setting a level overrides the default
Any explicit --test-level wins over the default, with one restriction. Production does not accept NoTestRun for a deployment that includes Apex.
sf project deploy start --source-dir force-app --target-org my-sandbox --test-level NoTestRunThis command is fine against a sandbox. It behaves the same as the default there, and the explicit flag simply states the intent. Pointed at production with Apex in the package, the same flag does not give you a test-free deployment.
You can also go the other way and tighten the sandbox default. This is useful for a CI validation job:
sf project deploy start --source-dir force-app --target-org my-sandbox --test-level RunLocalTestsNow the sandbox runs the same tests production would, so failures show up before release day.
For a targeted run, name the classes yourself:
sf project deploy start --source-dir force-app --target-org my-prod --test-level RunSpecifiedTests --tests AccountServiceTestA worked scenario
A team keeps this Apex class in force-app, along with its test class from the earlier chapters:
public with sharing class AccountService {
public static Integer countByIndustry(String industry) {
return [SELECT COUNT() FROM Account WHERE Industry = :industry];
}
}
@IsTest
private class AccountServiceTest {
@IsTest
static void countsMatchingAccounts() {
insert new Account(Name = 'Acme', Industry = 'Energy');
Test.startTest();
Integer result = AccountService.countByIndustry('Energy');
Test.stopTest();
Assert.areEqual(1, result, 'Expected one Energy account');
}
}A developer changes AccountService and deploys to a sandbox with no flags. It succeeds in seconds because NoTestRun applied. Nobody finds out that another test in the org, say OpportunityServiceTest, now fails because of a shared data assumption.
The release manager then deploys the same source to production with no flags. RunLocalTests applies, OpportunityServiceTest runs, it fails, and the deployment fails. The failure shows up in production, at release time, which is the worst place to find it.
The fix is to stop relying on the sandbox default. The team sets --test-level RunLocalTests in the CI deployment to the integration sandbox, so the failing test appears at merge time.
Common mistakes
- Assuming a green sandbox deploy means the tests pass. With the default, no tests ran.
- Assuming production always runs tests. A package without Apex runs none.
- Assuming the default depends on the org's contents. It depends on the deployment package.
- Treating a Developer Edition or scratch org like production. They follow the non-production default.
- Leaving the level out of automation. An omitted flag hides intent and makes reviews harder. Write the level in every pipeline command.
Quick reference
- Non-production target: NoTestRun, whatever the package holds.
- Production with Apex classes or triggers: RunLocalTests.
- Production without Apex: NoTestRun.
- An explicit
--test-leveloverrides the default, but production won't accept NoTestRun for Apex deployments. - Confirm what ran by checking for test results in the deployment output.
Exercise
Your pipeline runs sf project deploy start --source-dir force-app --target-org my-prod with no test level. In one release the package contains only a new custom object and two page layouts. In the next release it also contains a modified Apex trigger. Which test level applies to each release, and why?
Show answer
The first release has no Apex classes or triggers, so the default is NoTestRun and no tests run. The second release contains an Apex trigger and the target is production, so the default is RunLocalTests. All local tests in the org run, excluding tests from installed managed packages. The default depends on the package contents, not on what already exists in the org.
Exercise
A developer says, "My deployment to the sandbox passed, so our tests are fine." What is wrong with that claim, and what single flag would make the sandbox deployment a better check?
Show answer
With no test level, a sandbox deployment uses NoTestRun, so no tests ran and the result says nothing about test outcomes. Adding --test-level RunLocalTests makes the sandbox run the same local tests that production would run by default.