Sub topic 10
Coverage requirements for each level
This chapter assumes you can write a basic @IsTest class and run sf project deploy start. It also assumes you know how sandboxes differ from production. The question here is how much of your Apex must be covered by tests, and how that depends on the test level.
What coverage counts
Code coverage is the share of executable Apex lines that ran during tests. Salesforce calculates it as covered lines divided by covered plus uncovered lines. Comments, blank lines and test classes are not counted.
The threshold is 75%. A deployment to production that includes Apex must meet it. Also, every trigger in the deployment needs some coverage above 0%.
The rule for each level
The test level decides which tests run. It also decides which classes the 75% rule is measured against.
| Test level | Which tests run | Coverage rule |
|---|---|---|
| NoTestRun | None | No coverage check. Not accepted for production deployments that include Apex. |
| RunSpecifiedTests | Only the tests you name | Each class and trigger in the deployment must reach 75% from those tests. |
| RunLocalTests | All tests in your org, excluding installed managed packages | 75% across all the org's Apex, and triggers above 0%. |
| RunAllTestsInOrg | Every test, including managed package tests | 75% across all the org's Apex, and triggers above 0%. |
The difference between RunSpecifiedTests and the two broader levels matters most. With RunSpecifiedTests, the check is per class. One poorly covered class in the package fails the deployment, even if the org as a whole is well covered. With RunLocalTests, a weak class can be offset by well-covered ones elsewhere in the org.
Seeing the difference
Suppose your package contains AccountService (90% covered) and InvoiceService (50% covered). Your org has many other well-covered classes.
sf project deploy start --source-dir force-app --target-org my-prod --test-level RunSpecifiedTests --tests AccountServiceTestThis fails. InvoiceService is below 75% from the named tests, and the per-class rule gives it no credit for the rest of the org. Running RunLocalTests instead could pass, because the org-wide figure may stay above 75%.
To fix the first command, add a test that exercises InvoiceService and name it too. Repeat --tests for each class:
sf project deploy start --source-dir force-app --target-org my-prod --test-level RunSpecifiedTests --tests AccountServiceTest --tests InvoiceServiceTestWhere the rules are enforced
Coverage rules apply to production deployments. Sandbox deployments do not enforce the 75% threshold, so a sandbox deployment can pass with low coverage. The same code can still fail when you promote it to production.
For that reason, check coverage in a CI validation before the production release. Do not wait for the production deployment to reveal the problem.
Common mistakes
- Treating a green sandbox deployment as proof that production will accept the code.
- Naming only the test class for the class you changed, when other deployed classes also need 75%.
- Writing tests that call a method but assert nothing. They count toward coverage but check no behavior.
- Assuming the org-wide figure protects a new trigger. A trigger with no coverage still fails.
Exercise
A production deployment contains ClassA at 95% coverage and ClassB at 60% coverage. The org as a whole is at 85%. Does the deployment pass with RunSpecifiedTests? Does it pass with RunLocalTests?
Show answer
With RunSpecifiedTests it fails, because each deployed class must reach 75% and ClassB is at 60%. With RunLocalTests it can pass, because the check is org-wide and the org is at 85%, assuming all tests pass.