Contents
How does code coverage work in Salesforce apex?
Coverage is based on the total number of code lines in the organization. Adding or deleting lines of code changes the coverage percentage. For example, let’s say an organization has 50 lines of code covered by test methods. If you add a trigger that has 50 lines of code not covered by tests, the code coverage percentage drops from 100% to 50%.
How to get Apex code coverage statistics when using APEX?
The easiest way to be able to view test coverage is to have set the –codecoverage flag ( -c for short) to retrieve code coverage when you run the test command. To save the output so you can go back to it, I’d suggest the following steps.
What are the unit test requirements for apex?
In addition to ensuring the quality of your code, unit tests enable you to meet the code coverage requirements for deploying or packaging Apex. To deploy Apex or package it for the Salesforce AppExchange, unit tests must cover at least 75% of your Apex code, and those tests must pass.
How many test lines are covered by Apex trigger?
The trigger increases the total code lines in the organization from 50 to 100, of which only 50 are covered by tests. When Apex is deployed to production or uploaded as part of a package to the Salesforce AppExchange, Salesforce runs local tests in the destination organization.
What happens when code coverage is less than 75%?
When deploying new components that have 100% coverage to production, the deployment fails if the average coverage between the new and existing code doesn’t meet the 75% threshold.
How is code coverage calculated for managed packages?
Although managed package tests cover lines of code in managed packages, this coverage is not part of the organization’s code coverage calculation as total lines and covered lines. In contrast, the code coverage computed in a deployment after running all tests through the RunAllTestsInOrg test level includes coverage of managed package code.
When to increase code coverage in sandbox or production?
Sandbox and production environments often don’t contain the same data and metadata, so the code coverage results don’t always match. If code coverage is less than 75% in production, increase the coverage to be able to deploy or upload your code.