Code coverage inconsistency between deployments and "Estimate your organization's code coverage"
Last updated 2022-02-10 ·Reference W-2387383 ·Reported By 10 users
Summary
When you are running a deployment or creating a package, you are receiving an error stating that your code coverage is below 75%. You then clear all test classes in your Organization, run all tests, and click " Estimate your organization's code coverage". This is showing >75% code coverage for the entire Organization.
When we run a deployment we run all tests and it always returns the correct code coverage value. When running all tests within the UI there can be missing entries being calculated towards the "Estimate your organization's code coverage"
If you have an Apex Class that is not covered by a test Class, this may not be contributing towards the estimated code coverage value, giving your Org a higher code coverage value than it's true value. When we create apex classes via Setup/Developer console, a value is automatically created in ApexCodeCoverageAggregate. The estimate value is calculated from entries within this table. If a value from this table is deleted (via Tooling API) or missing, the estimated value returned will be incorrect for your Organization.
Repro
1) In a fresh org create these classes:
global class ClassA {
public void method1() {
System.debug('line1');
}
public void method2() {
System.debug('line2');
}
}
@isTest
global class ClassATest {
static ClassA a = new ClassA();
public static testMethod void testMethod1() {
a.method1();
}
public static testMethod void testMethod2() {
a.method2();
}
}
global class ClassB {
public void method3() {
System.debug('line3');
}
}
global class ClassC {
public void method4() {
System.debug('line4');
}
}
NOTE: ClassA has 100% coverage, while ClassB & C have 0% coverage (same number of methods, so overall the Org's coverage is 50%)
2) In the Developer console run this Tooling API query
SELECT Id, ApexClassOrTriggerId, ApexClassOrTrigger.Name, NumLinesCovered, NumLinesUncovered FROM ApexCodeCoverageAggregate
3) Select all results and hit delete
4) Run all tests
5) Check Setup/Apex classes/Code coverage: you will see 100%
Workaround
- Identify all classes that don't have any tests defined and create Apex tests
- This will correctly compute the org's coverage
Is it Fixed?
Any unreleased services, features, statuses, or dates referenced in this or other public statements are not currently available and may not be delivered on time or at all. Customers who purchase our services should make their purchase decisions based upon features that are currently available.