Sub topic 7
RunLocalTests: your org's own tests
This chapter assumes you can read basic Apex, know what @IsTest does, understand the difference between a sandbox and production, and can run sf commands. It covers one test level: RunLocalTests.
What RunLocalTests runs
RunLocalTests runs every Apex test in the target org, except tests that come from installed managed packages. "Local" means code that belongs to your org: your own classes, plus tests from unmanaged packages and metadata you have deployed.
This is the level most teams use for production releases. It is also the default for a production deployment that contains Apex, so you get it even when you pass no flag.
How it differs from the neighboring levels
| Level | Tests that run | Managed package tests |
|---|---|---|
| RunSpecifiedTests | Only the classes you name | Not run |
| RunLocalTests | All tests in the org | Excluded |
| RunAllTestsInOrg | All tests in the org | Included |
Excluding managed package tests keeps the run shorter. It also keeps your deployment from failing because of code you cannot change.
Running it from the CLI
Pass the level with --test-level. No --tests flag is needed, because every local test runs.
sf project deploy start --source-dir force-app --target-org my-prod --test-level RunLocalTestsIf you also pass --tests with this level, the CLI rejects the command. The --tests flag belongs to RunSpecifiedTests.
Coverage rules
With RunLocalTests, the deployment is checked against the org-wide code coverage rule. The total coverage of all Apex in the org must be at least 75%. Every trigger in the deployment must also have some coverage.
Coverage comes from the tests that actually ran. A class counts as covered only when a passing test executes its lines. The sample from earlier in this tutorial shows the pattern:
@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');
}
}The test creates its own data and asserts a result. That is why the deployment can count AccountService as covered.
The flow of a run
Common errors
- A test fails that you did not touch. Because the whole org is tested, an unrelated test can fail your release. A common cause is a test that depends on existing org data instead of creating its own. Fix the test, not the deployment.
- Coverage falls under 75%. Adding a large class without tests lowers the org-wide percentage. The deployment fails even if your new code is small. Add tests for the new code, or for older classes, until the total recovers.
- Runs take too long. Every local test runs, so large orgs can take many minutes. Use validation followed by a quick deploy to avoid waiting at release time. A later chapter covers this.
Exercise
Exercise
You deploy one new class to production with --test-level RunLocalTests. An older test class, not included in your package, fails. What happens to your deployment, and why?
Show answer
The deployment fails and is rolled back. RunLocalTests runs every non-managed test in the target org, not only tests in your package, so the older failing test counts against the deployment.
Exercise
Org-wide coverage is 74% after your changes, and all tests pass. Does a RunLocalTests deployment to production succeed?
Show answer
No. The org-wide coverage must be at least 75%, so the deployment fails until you add enough test coverage.