Code Coverage
Code Coverage in Salesforce is the percentage of Apex code (triggers and classes) exercised by the org's Apex unit tests when those tests run.
Definition
Code Coverage in Salesforce is the percentage of Apex code (triggers and classes) exercised by the org's Apex unit tests when those tests run. Salesforce calculates coverage by tracking which executable code lines each test method touches and dividing by the total number of executable lines across all production code. The platform enforces a 75 percent overall coverage threshold for production deployments: any deploy that drops org-wide coverage below 75 percent fails with a coverage error. Individual triggers must hit at least 1 percent coverage. The rules are platform-enforced; they exist to ensure deployed code has been minimally tested.
Code Coverage matters because it is the only platform-enforced quality gate Salesforce applies to Apex. Without it, untested code could ship to production routinely. With it, every Apex change forces the developer to write at least minimum tests. The 75 percent threshold is widely criticised as too low to guarantee real quality; a class with 100 percent coverage and no assertions still passes the gate. Mature teams target 85 to 95 percent coverage with meaningful assertions, treating the platform's 75 percent as a floor rather than a target. Code Coverage shows up at production deploy time, in the Apex Test Execution page, and in the Code Coverage Setup page that lists coverage per class.
How Code Coverage is calculated and enforced
How Salesforce calculates coverage
When Apex tests run, Salesforce tracks which executable lines each test touches. Coverage is computed as covered lines divided by total executable lines, expressed as a percentage. Comments, declarations, and certain platform-generated code do not count. Each class and trigger has its own coverage; the org-wide number is the weighted average.
The 75 percent overall threshold
Production deployments require at least 75 percent overall code coverage across the org. If a deploy would drop coverage below 75 percent, the deploy fails. The rule applies to the combined coverage of all changing classes; a single class with low coverage can fail the gate if it brings the average down.
The 1 percent per-trigger rule
Every trigger must have at least 1 percent coverage. The rule exists because triggers without any test exercise are particularly risky; they fire on every record save. The 1 percent is a low floor but enough to require at least one test that touches the trigger.
Coverage versus quality
Coverage measures whether code executed, not whether the test asserted anything meaningful. A test that calls every method but checks no outcomes still produces high coverage. Mature teams pair coverage targets with assertion-quality reviews; the two together produce real test quality.
Where coverage data lives
The Apex Test Execution page shows coverage per run. Setup, Apex Test Execution, Code Coverage shows current coverage per class. The ApexCodeCoverage and ApexCodeCoverageAggregate standard objects are queryable via SOQL, which is how custom dashboards report coverage over time.
Strategies to maintain coverage
Mature teams target 85 to 95 percent coverage with meaningful assertions. Test classes mirror production classes (one test class per business class). The discipline pays off in defect prevention, not in passing the 75 percent gate. Continuous integration runs tests on every pull request, blocking merges that drop coverage.
Coverage in deployments and CI/CD
Production deploys via Salesforce DX or Change Sets include a coverage check. The deploy command can specify which tests to run (NoTestRun, RunSpecifiedTests, RunLocalTests, RunAllTestsInOrg). Production deploys typically use RunLocalTests, which runs all non-managed-package tests in the deploying org.
Common coverage pitfalls
Three patterns recur. Tests written purely to hit coverage without meaningful assertions produce high coverage but low quality. Coverage that drops over time because new code lands without new tests is invisible until the deploy gate hits. And managed package classes have their own coverage requirements that subscribers cannot control.
How to maintain meaningful Code Coverage
Hitting 75 percent coverage is the floor; treating it as a target is the start of test debt. Mature teams aim higher and pair coverage with assertion-quality reviews.
- Write test methods alongside production code
Every new class or trigger gets a corresponding test class. Treat tests as part of the implementation, not an afterthought.
- Use meaningful assertions
System.assertEquals, System.assertNotEquals, System.assert with explicit expected values. Tests without assertions raise coverage without raising quality.
- Run tests in CI on every change
Configure CI/CD to run Apex tests on every pull request. Block merges that drop coverage below the team's target (usually 85 to 95 percent).
- Monitor coverage over time
Query ApexCodeCoverageAggregate weekly. Build a dashboard showing trend; declining coverage is a signal to address before the production deploy gate enforces it.
- Investigate uncovered code paths
The Code Coverage page highlights uncovered lines. Investigate them; if production code is genuinely unreachable, simplify; if it is reachable, write the missing test.
- 75 percent is a floor, not a target. Tests at 75 percent without assertions provide no real quality assurance.
- Coverage that drops slowly is invisible until the deploy gate fails. Monitor trend, not just point-in-time numbers.
- Managed package classes have their own coverage; subscribers cannot improve it.
- Tests that only exercise code without asserting outcomes inflate coverage without preventing defects. Pair coverage with assertion reviews.
Trust & references
Cross-checked against the following references.
- Code Coverage in ApexSalesforce Developer Docs
- Apex TestingSalesforce Developer Docs
Straight from the source - Salesforce's reference material on Code Coverage.
- Apex Test ExecutionSalesforce Help
About the Author
Dipojjal Chakrabarti is a B2C Solution Architect with 29 Salesforce certifications and over 13 years in the Salesforce ecosystem. He runs salesforcedictionary.com to help admins, developers, architects, and cert/interview candidates sharpen their fundamentals. More about Dipojjal.
Test your knowledge
Q1. What is the minimum code coverage required to deploy Apex to production?
Q2. Why is coverage alone not a sufficient measure of test quality?
Q3. What is a good target for code coverage in mature teams?
Discussion
Loading discussion…