Sub topic 2
How Apex code coverage is measured
This chapter assumes you can read basic Apex and write a test method with @IsTest. It also assumes you know that a deployment can run those tests. Here you learn what the coverage percentage counts, so the later chapters on test levels and coverage rules make sense.
What coverage counts
Apex code coverage measures how many executable lines in your classes and triggers ran while tests executed. The formula is:
coverage % = covered lines / (covered lines + uncovered lines) × 100A few rules follow from this:
- Only executable lines count. Blank lines, comments and most braces and signatures are ignored.
- Test classes (those marked
@IsTest) do not count toward the total. - A line is covered if any test executed it at least once. Running it 100 times is no better than running it once.
- Assertions play no part. A test with no assertions can still produce 100% coverage.
The last point matters. Coverage tells you which code ran. It does not tell you whether the code behaved correctly.
A worked example
This class has one branch. It has three executable lines: the if, and the two return statements.
public with sharing class DiscountService {
public static Decimal apply(Decimal price, Boolean isPartner) {
if (isPartner) {
return price * 0.9;
}
return price;
}
}A test that covers only the partner path runs two of the three lines, so coverage is about 67%.
@IsTest
private class DiscountServiceTest {
@IsTest
static void partnerGetsDiscount() {
Decimal result = DiscountService.apply(100, true);
Assert.areEqual(90, result, 'Partners get 10% off');
}
}The final return price; never runs. Adding a second test method for the non-partner path covers it and brings the class to 100%.
@IsTest
static void nonPartnerPaysFullPrice() {
Decimal result = DiscountService.apply(100, false);
Assert.areEqual(100, result, 'Non-partners pay full price');
}Per-class and org-wide numbers
Salesforce tracks coverage at two levels:
| Scope | What it measures |
|---|---|
| Per class or trigger | Covered lines divided by executable lines in that one file |
| Org-wide | Covered lines across all classes and triggers divided by all executable lines |
The org-wide figure is a weighted total, not an average of per-class percentages. A large class with poor coverage pulls the total down more than a small one. Which of the two numbers a deployment checks depends on the test level. That is the subject of the coverage rules chapter.
Where the numbers come from
Coverage is not stored as a permanent property of your code. A test run produces it. The deployment runs the selected tests, records which lines executed, and then compares the result with the required threshold.
Use the diagram below as a summary of that flow.
Because the numbers come from the tests that actually ran, a deployment that runs fewer tests can report lower coverage for the same code.
Common mistakes
- Assuming a test class counts toward the total. It does not, so adding test code never raises coverage by itself.
- Covering only the happy path. Branches such as
elseblocks and exception handlers stay uncovered until a test triggers them. - Relying on org data. Tests should create their own records, as in
AccountServiceTest. Otherwise results can change between orgs.
Exercise
A class has 20 executable lines. Its tests run 15 of them, and each of those 15 lines runs several times. What is the coverage, and does adding an assertion to a test change it?
Show answer
Coverage is 15 / 20 = 75%. Running a line several times does not matter, only whether it ran at least once. Adding an assertion does not change the number, because coverage counts executed lines and ignores what the test checks.