Sub topic 3
How coverage is counted: executed lines, not verified behavior
This chapter assumes you can read basic Apex (classes, methods, SOQL, DML) and have an org where you can run tests. It explains what the coverage number measures and what it ignores.
What the platform counts
Apex coverage is line coverage. Salesforce records which executable lines in your classes and triggers ran while tests were running. The percentage is:
executed lines ÷ (executed lines + lines never executed)
Several things follow from this definition:
- Only executable statements count. Comments, blank lines, and class or method declarations are not in the total.
- Test classes do not count. Code in
@isTestclasses is excluded from both sides of the calculation. - A line counts as covered if it ran at least once. Running it 200 times or once makes no difference.
- Nothing about the result is recorded. A line is covered whether the code was right or wrong.
A worked example
Take the DiscountCalculator class from this tutorial. Its executable lines include the null check, the range check, the two throw statements, and the return.
public with sharing class DiscountCalculator {
public static Decimal apply(Decimal amount, Decimal percent) {
if (amount == null || percent == null) {
throw new IllegalArgumentException('amount and percent are required');
}
if (percent < 0 || percent > 100) {
throw new IllegalArgumentException('percent must be between 0 and 100');
}
return amount - (amount * percent / 100);
}
}One test calling DiscountCalculator.apply(100, 10) runs both if conditions and the return. Neither throw line runs. The two throw lines stay uncovered, and the report lists them.
A second test calling apply(100, 150) runs the second throw. Only the first throw is still uncovered. Calling apply(null, 10) would cover it and bring the class to full line coverage.
Coverage ignores assertions
Now look at the weak test from earlier.
@isTest
private class WeakDiscountCalculatorTest {
@isTest
static void coversLinesButChecksNothing() {
DiscountCalculator.apply(100, 10);
}
}This test covers the happy path lines. It checks nothing. If someone changed the formula to amount + (amount * percent / 100), the test would still pass and the coverage number would stay the same. The platform reports what ran, not whether the output was correct.
Gaps in what line coverage shows
Line coverage is coarse, and several things can hide inside a "covered" line.
- Partial conditions. In
amount == null || percent == null, a test withamount = nullcovers the line. It never runs thepercent == nullhalf. The line shows as covered anyway. - Ternary expressions. A line such as
Math.mod(i, 2) == 0 ? 2000000 : 500is covered when either branch runs. - Empty or trivial paths. A
catchblock that swallows an error is covered the moment an exception reaches it. - Unrelated code. Coverage is credited to whatever code runs during a test, including code the test was not meant to exercise, such as a trigger that fires during test data setup.
Where the number is calculated
Salesforce reports coverage per class and per trigger, plus an org-wide figure. The org-wide figure is the sum of covered lines across all code, divided by the sum of all counted lines. Large, well-covered classes can therefore raise the total and mask a small class with 0% coverage.
Using the number correctly
Treat uncovered lines as a to-do list. If a line is red in the report, no test has run it, so nobody would notice if it broke. Treat covered lines as unproven until an assertion checks their outcome.
Exercise
A class has 10 executable lines. One test runs 8 of them and has no assertions. What coverage does Salesforce report, and does it say the 8 lines work correctly?
Show answer
It reports 80% coverage (8 of 10 lines executed). It says nothing about correctness, because coverage records only that lines ran. Without assertions, the test would pass even if those 8 lines returned wrong results.