Loading
Salesforce Enforces New Security Requirements in Summer 2026Read More

Code coverage steps before deployment

Publish Date: Jun 14, 2026
Description

Before deploying Apex code to a Salesforce production or sandbox environment, it is critical to accurately assess the current state of code coverage in the destination org. Inaccurate coverage data can cause deployment failures even when tests pass in the source org. This article provides a step-by-step process to clear test history, recompile all Apex classes, run all tests, and validate the estimated coverage using the ApexOrgWideCoverage object — all prior to deployment. It also explains how adding new Apex code affects the overall coverage calculation.
Code coverage indicates how many lines of Apex code are executed when tests run. Salesforce requires a minimum of 75% code coverage across the entire org for a deployment to succeed. The steps below help ensure your coverage data is accurate before attempting a deployment.

 

Resolution

How to Accurately Calculate Code Coverage Prior to Deployment

Follow these steps in the destination org before performing or troubleshooting any deployment-related code coverage issues.


Step 1: Clear the test history. Navigate to Setup > Apex Test Execution > click "View Test History" > click "Clear Test Data". This deletes all entries from the ApexCodeCoverage table and resets all entries in the ApexCodeCoverageAggregate table to 0 lines covered. Your org will temporarily show 0% code coverage.
Step 2: Recompile all Apex classes. Navigate to Setup > Develop > Apex Classes > click "Compile all classes".
Step 3: Run all tests. Navigate to Setup > Develop > Apex Classes > click "Run All Tests". All Apex tests in the org will run. Note: the "Run All Tests" button ignores any active view filter — it runs all tests in the org.
Step 4: After all tests complete, estimate the org's code coverage. Navigate to Setup > Develop > Apex Classes > click "Estimate your organization's code coverage".
Step 5: Note the estimated coverage percentage.
Step 6: Verify using the Tooling API. In Developer Console, open the Query Editor tab and select "Use Tooling API". Run the following query to retrieve the organization-wide coverage percentage from the ApexOrgWideCoverage table:
SELECT PercentCovered FROM ApexOrgWideCoverage
The PercentCovered field returns the organization-wide Apex code coverage percentage. This result should match the estimate from Step 4-5.
Step 7: Perform a "dummy" deployment validation. In a source sandbox, create a changeset containing no Apex code (add only a non-Apex component such as a custom field to force a run of all tests). Upload this changeset as an outbound changeset, then validate it in the destination org under Inbound Change Sets. The coverage calculated should match Steps 5 and 6. If it is below 75%, the deployment will fail due to a code coverage violation.
Step 8: Note the results. You now have an accurate picture of the current code coverage state in the destination org.

How New Code Being Deployed Impacts Code Coverage

When deploying new Apex code, overall coverage is calculated as: (existing lines covered + new lines covered) / (existing total lines + new total lines)
Example calculation: If the destination org currently has 70% coverage with 10,000 lines (7,000 covered), and you are deploying a new Apex class with 95% coverage and 100 lines (95 covered), the new overall coverage is: (7,000 + 95) / (10,000 + 100) = 70.25%. The deployment would still fail if this is below 75%.
To validate: create a changeset in a source sandbox containing only the new Apex class. Validate it in the destination org. The coverage result should reflect the calculation above.

Additional Code Coverage Considerations

  • Ensure all tests pass and at least 1% of coverage is applied to all triggers.
  • If coverage has decreased since the last deployment, follow Steps 1–8 above and review all test classes for @isTest(SeeAllData=true), which can introduce metadata dependencies that reduce coverage.
  • Identify any test classes using API version 23.0 or lower — these default to @isTest(SeeAllData=true) behavior.
Knowledge Article Number

000386953

 
Loading
Salesforce Help | Article