Sub topic 4
The Four Test Levels at a Glance
This chapter assumes you can read basic Apex, including the @IsTest annotation. It also assumes you know how sandboxes differ from production and can run sf commands. It introduces the four deployment test levels so the later chapters on defaults, coverage and pipelines have a shared reference.
What a test level controls
A deployment test level tells Salesforce which Apex tests to run before it commits your changes. If the tests fail, the deployment fails. The level changes two things:
- Which test classes run.
- How much work the deployment does before it can succeed.
Salesforce defines four levels. They run from no tests to every test in the org.
The levels compared
| Level | What runs | Typical use |
|---|---|---|
| `NoTestRun` | No Apex tests | Fast feedback in a sandbox or scratch org |
| `RunSpecifiedTests` | Only the test classes you name | Targeted checks on a small change |
| `RunLocalTests` | All tests in the org, except tests from installed managed packages | Broad validation of your own code |
| `RunAllTestsInOrg` | All tests, including those from managed packages | Full regression check |
NoTestRun
Nothing executes, so this level is the fastest. Salesforce does not accept it for production deployments that include Apex. Use it where speed matters more than verification.
RunSpecifiedTests
You list the test classes, and only those run. You choose the tests, so you also own the risk that an unlisted test would have failed. This level has its own coverage rules, which a later chapter covers.
RunLocalTests
Every test that belongs to your org's own code runs. Tests that ship inside installed managed packages are left out. That keeps the run focused on code you control.
RunAllTestsInOrg
This runs everything RunLocalTests runs, plus the tests from managed packages. It is the slowest level. It is also the one most exposed to failures you did not cause.
Choosing a level in the CLI
You set the level with the --test-level flag on sf project deploy start. The flag takes the level name exactly as written in the table.
sf project deploy start --source-dir force-app --target-org my-sandbox --test-level RunLocalTestsWhen you use RunSpecifiedTests, you add --tests with the class names. Later chapters cover that flag and the default level used when you set none.
What stays the same across levels
Test levels decide which tests run. They do not change what a good test looks like. A test class still creates its own data and asserts a result:
@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 test like this can run under RunSpecifiedTests, RunLocalTests or RunAllTestsInOrg. Only the set of tests selected changes.
Common mistakes
- Assuming
RunLocalTestsincludes managed package tests. It does not. OnlyRunAllTestsInOrgdoes. - Passing
--testswithoutRunSpecifiedTests. The named classes belong with that level. - Treating the level names as case-flexible in scripts. Copy them exactly.
Exercise
A team wants a CI check that runs every test written in their own org but ignores tests from installed managed packages. Which level should they pass to --test-level, and which level would also run the managed package tests?
Show answer
They should use RunLocalTests. RunAllTestsInOrg would also run the managed package tests.
Exercise
You deploy to production with --test-level NoTestRun, and the deployment includes an Apex class. What should you expect, and what is one level you could use instead?
Show answer
Expect Salesforce to reject it, because NoTestRun is not accepted for production deployments that include Apex. You could use RunLocalTests or RunSpecifiedTests instead.