Sub topic 10
Coverage requirements for each level
This chapter assumes you can read basic Apex, know what @IsTest does, and have run sf project deploy start before. It covers how the 75% code coverage rule applies to each of the four test levels. That determines whether a production deployment passes.
What coverage measures
Apex code coverage is the share of executable lines in your classes and triggers that ran while tests executed. For a single class:
coverage = covered lines / (covered lines + uncovered lines)A few details affect the numbers:
- Only executable lines count. Blank lines and comments do not.
- Test classes (marked
@IsTest) are excluded from the calculation. - Coverage comes from the tests that ran in that deployment. A class that a test exercised last week gets no credit unless a test in the current run exercises it again.
- Coverage measures execution, not correctness. A test with no assertions can still cover every line.
The org-wide figure is the same formula applied to all Apex in the org. That means summing the covered and uncovered lines of every class and trigger, not averaging per-class percentages. A 10-line class and a 1,000-line class do not weigh the same.
The two rules to check
Every level that runs tests enforces two conditions, and the coverage rule differs by level:
- Every test that runs must pass.
- The coverage threshold for that level must be met.
NoTestRun runs nothing, so neither condition is checked for Apex.
| Level | Tests that run | Coverage rule |
|---|---|---|
| NoTestRun | None | Not checked |
| RunSpecifiedTests | Only the tests you name | Each class and trigger in the deployment needs at least 75% on its own |
| RunLocalTests | All tests in your org, except managed package tests | At least 75% across the org, and every trigger needs some coverage |
| RunAllTestsInOrg | All tests in the org, including managed package tests | At least 75% across the org, and every trigger needs some coverage |
The difference between the second row and the last two rows is the most common source of surprise.
NoTestRun: no coverage check
With NoTestRun, the deployment runs no Apex tests, so Salesforce computes no coverage and enforces no threshold. This is why it is the default for sandboxes and fast for development.
Production is different. A production deployment that contains Apex classes or triggers cannot use NoTestRun. Salesforce rejects it, or in practice runs the default level for production instead. A production deployment that contains only non-Apex metadata, such as page layouts or custom fields, does not trigger the Apex test requirement.
sf project deploy start --source-dir force-app --target-org my-sandbox --test-level NoTestRunRunning this against a sandbox deploys the code without checking coverage. Code that fails the 75% rule can still land in a sandbox, then fail later when you promote it.
RunSpecifiedTests: coverage per component
RunSpecifiedTests is the strictest in one respect. Each Apex class and trigger in the deployment must reach 75% on its own, and that coverage must come from the tests you listed.
Take a deployment of AccountService with --tests AccountServiceTest:
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');
}
}The test calls the only executable method in AccountService, so the class is fully covered. The deployment passes both conditions.
sf project deploy start --source-dir force-app --target-org my-prod --test-level RunSpecifiedTests --tests AccountServiceTestThe following points trip people up:
- If your package contains five classes and you name a test that covers only one of them, the other four have 0% coverage in this run. The deployment fails, even if the org as a whole is at 90%.
- A class that already has good coverage from other tests in the org gets no credit for it. Only the named tests count.
- A test class that is itself in the deployment package does not need coverage. Test classes are excluded.
- Naming a test that does not exist, or misspelling the class name, produces an error rather than silently running nothing.
Specify one test class for each component you deploy, or one that exercises several of them. Repeat --tests to name more classes.
RunLocalTests: the org-wide rule
RunLocalTests runs every test in your org except tests that belong to installed managed packages. Coverage is judged across all Apex in the org that is not part of a managed package. The requirement is 75% overall.
A worked example. Suppose the org has three classes:
| Class | Executable lines | Covered lines | Per-class coverage |
|---|---|---|---|
| `AccountService` | 20 | 20 | 100% |
| `OrderProcessor` | 100 | 80 | 80% |
| `LegacyImporter` | 80 | 30 | 37.5% |
The org total is 130 covered lines out of 200 executable lines, which is 65%. The deployment fails the 75% rule, even though two of the three classes are individually fine. Lifting LegacyImporter to 60 covered lines raises the total to 160 out of 200, which is 80%, and the deployment passes. LegacyImporter would still be at 75% in that case, but the rule only needs the total to reach 75%.
Two further consequences:
- A low-coverage class does not block a deployment by itself, as long as the org total stays at or above 75%. This lets old classes drag the number down without failing it.
- Triggers are the exception. Every trigger must have some coverage, meaning more than 0%. A trigger with no test touching it fails the deployment regardless of the org total.
Because the total covers the whole org, deploying new, poorly tested code can push you under 75% even if you did not change anything else. The reverse also happens: deploying a well-tested class can lift the total.
RunAllTestsInOrg: same threshold, wider scope
RunAllTestsInOrg applies the same org-wide 75% rule and the same trigger rule as RunLocalTests. It differs in which tests run: it adds the tests from installed managed packages.
That has practical effects:
- It takes longer, sometimes much longer, because it runs tests you did not write.
- Managed package tests can fail for reasons that are not yours, such as data in the org that the package author did not anticipate. That fails your deployment.
- Managed package code does not count toward your coverage figure, so running its tests does not raise your percentage.
Use it when you specifically want to confirm that your changes do not break a package. For the coverage gate alone, RunLocalTests is enough.
How the levels relate
The following diagram summarizes where the threshold is measured at each level.
Checking coverage before you deploy
To avoid finding a shortfall during a production release, run the tests against a sandbox or scratch org first and read the coverage report.
sf apex run test --target-org my-sandbox --test-level RunLocalTests --code-coverage --result-format human --wait 20The output lists coverage per class and the org-wide percentage. Compare the org-wide figure with 75% for RunLocalTests, and each component you deploy with 75% for RunSpecifiedTests. Expect small differences from the production result, because production data and installed packages can change which tests pass.
Common mistakes
- Assuming a passing RunLocalTests run implies each class is at 75%. It only implies the org total is.
- Deploying one class with RunSpecifiedTests but naming a test for a different class.
- Forgetting that a new trigger needs a test that fires it, such as one that inserts a record of the triggering object.
- Reading a sandbox result as proof. A sandbox deployment using NoTestRun says nothing about coverage.
Exercise
A production deployment contains the classes ClassA, ClassB and ClassC, with RunSpecifiedTests and --tests ClassATest. ClassATest covers 100% of ClassA but never calls ClassB or ClassC. The org-wide coverage is 88%. Does the deployment pass the coverage rule? What changes if you switch to RunLocalTests, assuming the org-wide figure stays at 88% and the tests pass?
Show answer
With RunSpecifiedTests it fails. Each class in the package needs 75% coverage from the named tests, and ClassB and ClassC have 0% in this run. The org-wide 88% does not matter at this level. With RunLocalTests the org-wide figure of 88% meets the 75% threshold, so the coverage rule passes. This holds as long as every trigger has some coverage and all tests pass.
Exercise
An org has 300 executable lines of Apex, with 195 covered. You deploy a new 100-line class with 40 lines covered, using RunLocalTests. Does the deployment pass the coverage rule?
Show answer
No. The new total is 235 covered lines out of 400, which is 58.75%. That is under 75%, so the deployment fails.