Sub topic 3
Code coverage rules that gate production
This chapter assumes you can run sf commands against an authorized org. It also assumes you know what Apex tests are and how the test levels work. The question here is which coverage numbers a production deployment must reach, and how to check them before you validate.
Where coverage is enforced
Code coverage is the share of executable Apex lines that your tests ran. Salesforce counts lines in classes and triggers. It ignores comments, blank lines, and test classes.
Coverage gates deployments to production orgs only. Deployments to sandboxes and scratch orgs do not require coverage, although failing tests still fail the deployment.
The rules
The rules depend on the test level you chose.
| Test level | Coverage rule in production |
|---|---|
| RunLocalTests | Overall org coverage must be at least 75%. Every trigger needs some coverage. |
| RunAllTestsInOrg | Same rules as RunLocalTests, but managed package tests are also run. |
| RunSpecifiedTests | Each class and trigger in the deployment must have at least 75% coverage from the tests you listed. |
| NoTestRun | Allowed only if the package has no Apex. |
Three details cause most surprises:
- Overall coverage is an org-wide figure. It covers all Apex in production, not only what you deploy. Your new class can have 100% coverage and the deployment can still fail if older code drags the total below 75%.
- Every trigger must be covered. Salesforce requires at least 1% coverage per trigger. A trigger with no test that fires it blocks the deployment.
- A failing test fails the deployment. Coverage is only checked after all selected tests pass.
How the check flows
Checking coverage before you validate
Measure coverage in production first. The Tooling API exposes the org-wide figure. Run this command to read it:
sf data query \
--use-tooling-api \
--query "SELECT PercentCovered FROM ApexOrgWideCoverage" \
--target-org prodThe result is the percentage from the most recent test runs in that org. It can be stale. If nobody has run the full suite recently, the number may not match what a new validation computes.
To find the weak spots, list coverage per class and trigger:
sf data query \
--use-tooling-api \
--query "SELECT ApexClassOrTrigger.Name, NumLinesCovered, NumLinesUncovered FROM ApexCodeCoverageAggregate ORDER BY NumLinesUncovered DESC" \
--target-org prodClasses at the top have the most uncovered lines. Improving them raises the org total fastest.
Why this matters for quick deploy
Quick deploy skips the tests, so it relies on the validation having passed the coverage gate. If the validation passed with the required test level and coverage, the quick deploy can reuse that result. A validation that fails coverage never produces a job ID you can quick deploy. Fix coverage first, then validate again.
Coverage in the validation is computed with the code as it will exist after the deployment. Your package's Apex replaces the current versions in that calculation. Deleting well-tested code, or adding a large untested class, changes the total.
Common errors
- "Average test coverage across all Apex Classes and Triggers is 74%, at least 75% required." The org-wide rule failed. Add tests for the largest uncovered classes, not only for your own changes.
- "Code coverage warnings: Trigger X: Test coverage of selected Apex Trigger is 0%, at least 1% required." No test fires the trigger. Add a test that performs the DML operation the trigger listens for.
- A class is below 75% under RunSpecifiedTests. Add the test class that covers it to the
--testslist. - Coverage looks fine in the sandbox but fails in production. The sandbox total counts different Apex than production. Always check the numbers in the target org.
Exercise
Your org-wide coverage in production is 82%. You deploy a new class with 60% coverage using RunLocalTests. Will the coverage gate pass? What changes if you use RunSpecifiedTests with only the new class's test?
Show answer
With RunLocalTests, the gate checks the org-wide figure after your deployment, so it likely still passes, because one class at 60% barely moves 82%. Every trigger must also have coverage, which is unaffected here. With RunSpecifiedTests, each class in the package must reach 75% individually, so the new class at 60% fails the gate.