Sub topic 4
The four test levels at a glance
Every deployment to a Salesforce org can run Apex tests, and the test level decides which ones. This chapter gives you the overview of the four levels. Later chapters cover coverage rules, defaults, CLI flags and failure handling in detail.
This chapter assumes you can read basic Apex and the @IsTest annotation. It also assumes you know the difference between a sandbox and production, and that you can run sf commands.
Why a test level exists
A deployment copies metadata into a target org. When that metadata includes Apex classes or triggers, Salesforce can run Apex tests as part of the deployment. The tests act as a gate: if a required test fails, or coverage is too low, the deployment is rejected and nothing is saved.
Running tests takes time, and the time grows with the size of the org. A sandbox deployment during development should be fast. A production release should be strict. The test level is the setting that lets you pick between those goals.
The four values are NoTestRun, RunSpecifiedTests, RunLocalTests and RunAllTestsInOrg. They are the same values in the Metadata API, the Salesforce CLI and most deployment tools.
The levels side by side
| Test level | Which tests run | Where it is allowed |
|---|---|---|
| `NoTestRun` | None | Sandboxes, Developer Edition and trial orgs. Not accepted for production deployments that include Apex. |
| `RunSpecifiedTests` | Only the test classes you name | Any org, including production |
| `RunLocalTests` | All tests in the org, except tests from installed managed packages | Any org, including production |
| `RunAllTestsInOrg` | Every test in the org, including tests from installed managed packages | Any org, including production |
The levels are ordered by how much they run. NoTestRun runs nothing. RunSpecifiedTests runs a chosen subset. RunLocalTests runs everything you own. RunAllTestsInOrg runs everything.
More tests give you more confidence and a longer wait. Fewer tests give you speed and less protection.
The sample class and its test
The examples in this chapter use one small class and its test class. The test creates its own data and asserts a result.
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');
}
}Test methods do not see the org's existing data by default. That is why this test inserts its own account. Tests that depend on records already in the org tend to pass in one sandbox and fail in another, and a deployment gate magnifies that problem.
The test also exercises AccountService. Salesforce counts the lines it executes toward the class's code coverage. The deployment uses that coverage figure for its 75% requirement, which a later chapter covers.
NoTestRun: skip the tests
NoTestRun runs no Apex tests. It is the fastest option, so it suits frequent deployments to a development sandbox or scratch-style environment where you only want to see whether the metadata is valid.
sf project deploy start --source-dir force-app --target-org my-sandbox --test-level NoTestRunProduction rejects this level for deployments that include Apex. The reasoning is simple: production is where bad code does the most damage, so Salesforce insists on a test run there.
Two consequences follow from skipping tests:
- The deployment cannot catch a broken test or a regression in code you did not touch.
- You may still deploy code with poor coverage. A later promotion to production will then fail at the coverage check, possibly at an inconvenient time.
RunSpecifiedTests: a targeted run
RunSpecifiedTests runs only the test classes you name. You supply the names with the --tests flag, which you can repeat to name more classes.
sf project deploy start --source-dir force-app --target-org my-prod --test-level RunSpecifiedTests --tests AccountServiceTestThis level is useful when a change touches a small part of the code base. Instead of waiting for thousands of tests, you run the ones that cover the changed classes.
It also has a stricter coverage rule. Each class and trigger in the deployment must reach 75% coverage on its own, using only the tests you named. Other tests in the org do not help. If you name a test class that does not exercise a deployed class, that class fails the check. Later chapters explain this rule fully.
Two edge cases come up often:
- If you choose
RunSpecifiedTestsand name no tests, the deployment has nothing to run. Always pair the level with at least one--testsvalue. - The names must match real test classes in the org or in your deployment package. A typo causes an error and the deployment does not proceed.
RunLocalTests: your org's own tests
RunLocalTests runs every test in the org except tests that come from installed managed packages. "Local" means code that belongs to you, including tests in your unmanaged packages and in the metadata you are deploying.
sf project deploy start --source-dir force-app --target-org my-prod --test-level RunLocalTestsThis is the usual choice for a production release. It checks that the whole org still works after the change, not only the part you touched. It leaves out managed package tests because you cannot edit that code, and a failure inside a vendor's package is not something you can fix.
The cost is time. In a mature org the full local suite can run for an hour or more. Test runs also compete for resources, so slow tests can slow the release window. Plan for this, and see the chapter on validate and quick deploy for a way to move the wait out of your release window.
RunAllTestsInOrg: everything
RunAllTestsInOrg runs all tests in the org, including those that ship inside installed managed packages.
sf project deploy start --source-dir force-app --target-org my-prod --test-level RunAllTestsInOrgUse it rarely. It is the slowest level, and it adds failure sources you do not control. A managed package test can fail because of your org's data or configuration, and you may not be able to change it. Teams typically use this level when they need a complete health check, for example before a major release or when diagnosing a problem that they suspect involves a package.
Which tests count as "local"
The line between local and managed tests causes most confusion at this level. The rule is based on where the test class came from:
- A test class in your own namespace, or with no namespace, is local.
- A test class that arrived with an installed managed package, which has its own namespace, is not local.
RunLocalTests therefore runs the first group only. RunAllTestsInOrg runs both groups.
Defaults when you set nothing
If you leave out --test-level, Salesforce chooses a level for you:
- A deployment to a sandbox or another non-production org defaults to
NoTestRun. - A deployment to production that contains Apex defaults to
RunLocalTests.
Because of the second rule, a command that works in your sandbox without any flag may start a long test run when you point it at production. State the level explicitly in scripts so the behavior is visible to the next reader. A later chapter on defaults covers the exact conditions, including production deployments without Apex.
What the levels share
Whatever level you choose, some behavior stays the same:
- A failing test that the level runs causes the whole deployment to fail, and Salesforce rolls back the changes.
- Tests run in the target org, not on your machine, so they see the target org's configuration.
- Tests that insert records do not leave them behind. Salesforce rolls back test data automatically.
Choosing quickly
A rough guide for the four levels:
- Use
NoTestRunfor fast, repeated deployments to a development sandbox. - Use
RunSpecifiedTestswhen you know exactly which tests cover the change and need a shorter run, such as in a CI validation of a small change. - Use
RunLocalTestsfor production releases and for pipeline stages that must prove the whole org still passes. - Use
RunAllTestsInOrgonly when you need managed package tests included.
The chapter on choosing a level for each pipeline stage expands this into a full plan.
Exercise
You deploy a small change to production and want the run to use only AccountServiceTest. Which test level and flags do you use? Also, what happens if you set --test-level NoTestRun for that production deployment, assuming it contains Apex?
Show answer
Use --test-level RunSpecifiedTests --tests AccountServiceTest. For a production deployment that includes Apex, NoTestRun is not accepted, so the deployment is rejected instead of running without tests.
Exercise
A deployment with RunLocalTests fails because a test inside an installed managed package is broken. Is this the expected behavior? Explain.
Show answer
No. RunLocalTests excludes tests from installed managed packages, so a managed package test would not run at that level. The failing test must be a local one. A managed package test would run only with RunAllTestsInOrg.