Sub topic 7
RunLocalTests: your org's own tests
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. It covers RunLocalTests, the test level you will meet most often in production releases.
What RunLocalTests runs
RunLocalTests runs every Apex test in the target org, except tests that come from installed managed packages. A "local" test is one that belongs to your org's own code. That includes:
- Test classes you wrote and deployed.
- Test classes in the same deployment, which are run even if they are new.
- Tests from unmanaged code and from unlocked packages, because that code is yours to change.
Tests that belong to managed packages have a namespace and are owned by the package vendor. RunLocalTests skips them. RunAllTestsInOrg is the level that includes them.
The key point is that the set of tests does not depend on what you are deploying. If you deploy one small trigger change, RunLocalTests still runs the whole local suite. That makes it a good safety net, and it makes it slow in orgs with thousands of tests.
When it applies
RunLocalTests is the default for a deployment to production that contains Apex classes or triggers. If you set no level, production uses it automatically. A deployment to production that contains no Apex does not need tests, so the default there is NoTestRun. Sandboxes and scratch orgs also default to NoTestRun.
You can still choose it explicitly anywhere. Choosing it for a sandbox is a common way to check a change before the production release. You can also pass it for a production deployment even though it is the default. Being explicit makes the intent clear in pipeline scripts.
Running it from the CLI
Set the level with --test-level. No --tests flag is needed, because the level decides which tests run.
sf project deploy start --source-dir force-app --target-org my-prod --test-level RunLocalTestsFor large orgs the deployment can take longer than the default wait. The --wait flag sets how many minutes the command waits before returning control to you. The deployment keeps running in the org either way.
sf project deploy start --source-dir force-app --target-org my-prod --test-level RunLocalTests --wait 120Do not pass --tests with RunLocalTests. The --tests flag belongs to RunSpecifiedTests. Mixing them is a common mistake in pipeline scripts that were copied from a different stage.
Previewing the result before you deploy
You can run the same set of tests without deploying anything. This shows you failures and coverage before you start a release.
sf apex run test --test-level RunLocalTests --code-coverage --result-format human --wait 60 --target-org my-prodThe result reflects what is already in the org. It does not include the changes you are about to deploy. Use it to find tests that fail for reasons unrelated to your change, such as data or configuration differences.
Coverage rules under RunLocalTests
Code coverage is the share of Apex lines that a test run executed. For RunLocalTests in production, two rules apply:
- The org-wide coverage must be at least 75%. This is the total of covered lines across all local Apex divided by all lines. Managed package code is not counted.
- Every trigger in the deployment must have some coverage, which means more than 0%.
The 75% figure is an org-wide average, not a per-class requirement. A single class with 40% coverage does not fail the deployment by itself, as long as the org total stays at or above 75%. This differs from RunSpecifiedTests, where each class and trigger in the deployment needs its own 75% from the named tests. The next chapters compare the levels side by side.
Coverage is calculated with your deployed code included. A large new class with little test coverage can pull the org total below 75% and fail a release that touches nothing else.
| Item | Counted in coverage? |
|---|---|
| Your classes and triggers | Yes |
| Test classes (`@IsTest`) | No |
| Managed package code | No |
| Comments and blank lines | No |
A passing example
This class and test are the kind of pair that satisfy the rules. The test creates its own data and asserts a result. Both the class and the test are part of the local suite, so RunLocalTests runs the test and counts the class as covered.
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');
}
}Because the test inserts its own account and does not depend on existing records, it gives the same result in a sandbox and in production. Tests that rely on @IsTest(SeeAllData=true) or on specific records are the usual source of "passes in the sandbox, fails in production" results.
What happens when a test fails
With RunLocalTests, a single failing test fails the whole deployment. For a production deployment, Salesforce rolls back all of the changes, so nothing is saved. The result lists each failing test with its class, method, and message, and the stack trace when one is available.
The failing test does not have to be related to your change. A test written two years ago can fail today because of a new validation rule, a changed picklist value, or a flow that now runs on insert. The deployment still stops. You then have three options:
- Fix the test, which is almost always the right choice.
- Fix the underlying configuration problem.
- Deploy the fix together with your change, so both arrive in one release.
Deleting or disabling the test hides the problem and lowers coverage. It can also push the org total under 75%.
Common failure causes
| Symptom | Likely cause | First thing to check |
|---|---|---|
| Deployment fails, one test listed | Test depends on data or configuration that differs from the sandbox | Does the test create its own data? |
| "Average test coverage across all Apex Classes and Triggers is 70%, at least 75% required" | Org total is below the minimum after your code was added | Add tests for the new class, or for older classes with low coverage |
| "Code coverage for trigger is 0%" | A trigger in the deployment has no test that fires it | Add a test that inserts or updates a record to fire the trigger |
| Deployment times out in the CLI | Large local suite exceeds the wait time | Raise `--wait` and check progress in Setup |
| Test passes alone, fails in the full run | Tests share state or lock rows when they run in parallel | Isolate data per test, or avoid shared records |
The coverage message says "across all Apex Classes and Triggers" because the number is org-wide. Read the percentage in the message, not just the failing class.
Speeding it up with validation and quick deploy
Because RunLocalTests is slow, you often do not want to run it during the release window. Run it earlier as a validation, which runs the tests and checks coverage without saving anything. Later, a quick deploy publishes the same validated changes without running the tests again.
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-prodA quick deploy works only if the validation passed with all tests and coverage satisfied. It must happen within the allowed window, which is 10 days after validation. If the org's code or configuration changed in a way that conflicts, you may need to validate again.
When to choose RunLocalTests
- Use it for production releases that contain Apex. It is the default and the expected level.
- Use it for CI validations of release branches, so problems appear before the release window.
- Avoid it for local development loops. It is too slow, and
RunSpecifiedTestsorNoTestRunin a sandbox gives faster feedback.
Exercise
You deploy a small trigger change to production with no test flags. The org has 800 local tests and 20 tests from an installed managed package. Which tests run, and what coverage rule applies?
Show answer
Production with Apex defaults to RunLocalTests, so all 800 local tests run and the 20 managed package tests do not. Org-wide coverage of local Apex must be at least 75%, and the trigger must have more than 0% coverage. If any test fails, the whole deployment is rolled back.
Exercise
A deployment with RunLocalTests fails with "Average test coverage across all Apex Classes and Triggers is 72%, at least 75% required". The only class you changed has 90% coverage. Why did it fail, and what are your options?
Show answer
The 75% rule is org-wide under RunLocalTests, so low coverage in other classes, or in a large new class, can pull the total below 75%. Add tests for the least covered classes, including the ones you did not change, until the org total reaches 75%. Then validate again.