Sub topic 2
Code coverage: what is required and what it does not prove
This chapter assumes you can write basic Apex classes, run SOQL and DML, and know what a test method looks like from the previous chapter. It explains the coverage number Salesforce checks, how that number is calculated, and why a high number can still hide broken code.
What Salesforce requires
Code coverage is the share of your Apex lines that ran during tests. Salesforce enforces three rules when you deploy to production or package code:
- At least 75% of all Apex lines in the org must be covered by tests.
- Every trigger must have some coverage. 0% is not accepted.
- All tests that run in the deployment must pass.
Sandboxes and scratch orgs do not enforce the 75% rule. The rule applies at the deployment gate to production.
How the number is calculated
Salesforce counts executable lines. Coverage is covered lines divided by the total of covered and uncovered lines. Some details that surprise people:
- Comments and blank lines are not counted.
- Classes marked
@IsTestare not counted, so test code never inflates or lowers the percentage. - Coverage is per line, not per branch. An
ifline counts as covered when it runs, even if only one outcome was ever taken. System.debugcalls count as executable lines.
A test that covers code and proves nothing
Consider a small class with a branch.
public class DiscountService {
public static Decimal applyDiscount(Decimal price, Boolean isMember) {
if (isMember) {
return price * 0.9;
}
return price;
}
}This test reaches 100% coverage of DiscountService:
@IsTest
private class DiscountServiceCoverageOnlyTest {
@IsTest
static void callsBothPaths() {
DiscountService.applyDiscount(100, true);
DiscountService.applyDiscount(100, false);
}
}It never checks a result. If someone changed 0.9 to 0.1, or deleted the member branch's return value logic, this test would still pass. Coverage tells you the lines ran. It does not tell you they produced the right answer.
Here is the same code with a test that verifies behavior:
@IsTest
private class DiscountServiceTest {
@IsTest
static void membersGetTenPercentOff() {
Decimal result = DiscountService.applyDiscount(100, true);
Assert.areEqual(90, result, 'Members should pay 90 percent of the price');
}
@IsTest
static void nonMembersPayFullPrice() {
Decimal result = DiscountService.applyDiscount(100, false);
Assert.areEqual(100, result, 'Non-members should pay the full price');
}
}Both tests give the same coverage. Only the second pair fails when the logic breaks.
Common mistakes
| Mistake | Why it is a problem | Better approach |
|---|---|---|
| Tests with no assertions | Coverage rises, but bugs pass unnoticed | Assert on a value, a record, or an exception |
| Testing only the happy path | Untested branches can run on the same line count | Add a test for each outcome of every branch |
| Stopping at 75% | The number is a minimum for deployment, not a quality target | Judge a test by what it asserts |
| Using `SeeAllData=true` to reach code | Tests depend on org data and fail elsewhere | Create your own test data |
| Large tests that call many methods | A failure does not show which behavior broke | One behavior per test method |
Using coverage the right way
Use coverage as a map of gaps. Open a class in the Developer Console or in your IDE after a test run and look at the uncovered lines. Each one is a path no test has exercised. Write a test for that path and assert on what it should do.
Then ask a second question for every covered line: if this line were wrong, would a test fail? If the answer is no, the line is covered but not tested.
Exercise
A class has 80% coverage and all tests pass, but none of the test methods contain an assertion. Does it meet the deployment requirement, and does it show the code works?
Show answer
It meets the 75% org-wide requirement if the rest of the org is similar, since coverage only counts lines that ran. It does not show the code works, because without assertions no test can fail on a wrong result. Add Assert calls that check the expected values.