When Is Code Coverage Calculated?
The first thing to know is that the code coverage is stored when your latest test finished running. Each time you run the tests in a class, an entry is made in the code coverage table for each class you touch with those tests. The overall code coverage percentage in your organization doesn’t include code coverage from managed package tests. The only exception is when managed package tests cause your triggers to fire.
Re-run all tests in your organization:
---------------------------------------------------------------------------------
1a) Setup | Develop | Apex Test Execution
b) View Test History
c) Clear test data.
2a) Setup | Develop | Apex Classes
b) Compile all classes
3a) Setup | Develop | Apex Test Execution
b) Select tests | "My Namespace" | Select all
c) Click run
4a) Setup | Develop | Apex Classes
b) Estimate your organization's code coverage
What is Code Coverage Table?
It is used to display those blue and red highlights showing you which lines in a class are covered and which are not.
Why the Code Coverage Result is not updated during Deployment?
You may have notified that tests are automatically executed when doing a deployment. But please note that the relevant results of this calculation is NOT stored in the code coverage tables. The reason code coverage results are not stored on deployment is to support rollback. The coverage is stored as data in its own transaction. When you run tests in the developer console, you are not persisting anything to the data store besides these records. With a deployment, you are modifying metadata in addition to running tests. If a deployment fails, the entire deployment is rolled back. If any coverage data were saved in a separate transaction, it could be pointing to lines that never existed and to classes that were never committed.
How can we get Aggregated Code Coverage result from SOQL?
We can using Tooling API to calculate the coverage individually for an Apex Class/ Apex Trigger/ overall Coverage. You can follow the steps mentioned below:
Open Developer Console > Execute the query with "Use Tooling API" checked
- How many lines are covered for a specific class/ trigger:
SELECT NumLinesCovered, NumLinesUncovered FROM ApexCodeCoverage WHERE ApexClassOrTriggerId =XXX
- Which lines are covered for a specific class or trigger:
SELECT Coverage FROM ApexCodeCoverage WHERE ApexClassOrTriggerId = XXX
- The current org-wide coverage:
SELECT PercentCovered FROM ApexOrgWideCoverage
Why there is bad code coverage data for Apex Classes? And how to eliminate it?
There may be times where we see a different code coverage value than the actual value. This might be caused due to bad code coverage data or aggregate results from previous test runs. You can follow the steps mentioned below to eliminate any bad code coverage data in your organization:
Open Developer Console > Execute the below query with "Use Tooling API" checked
Select all the returned rows and hit "Delete Row"
SELECT Id, NumLinesUncovered FROM ApexCodeCoverageAggregate WHERE NumLinesUncovered = NULL
No comments:
Post a Comment