Lesson 52 of 93 · 6 min read · Updated

Why Code Coverage Can Mislead: Same Coverage, Very Different Protection

Code coverage is the most quoted test metric, and one of the most misunderstood. It tells you which lines ran during the tests. It does not tell you whether any test checked what those lines produced. This lesson puts two tests side by side that give the same coverage for the loyalty points calculator, where one protects nothing and the other protects every rule, and shows how to use coverage as a tool instead of a grade.

Technical Requirements

  1. .NET 11 SDK
  2. xUnit 2.9.3
  3. FluentAssertions 7.2.2 (Apache-2.0; 8.x is commercial)
  4. A coverage collector, for example the one that ships with Microsoft.NET.Test.Sdk: dotnet test --collect "Code Coverage"
  5. The LoyaltyPointsCalculator class from the first lesson in this section

What Coverage Says and What It Does Not

A coverage report answers one question: was this line executed? It cannot answer the question you actually care about: was the result of this line verified?

The calculator has a guard clause, a threshold check, a tier switch, a campaign multiplier and an upper cap. Any test that calls Calculate with the right mix of inputs runs all of those lines. Whether the test then looks at the return value makes no difference to the coverage number.

That gap is easy to exploit by accident. A team is told to reach 80% coverage. The fastest way to get there is to call methods without asserting much. The number goes up, the dashboard turns green, and the suite catches no more bugs than before.

High Coverage, Zero Protection

Here is the extreme case:

M06.BestPractices.Tests/CoverageAndMutationTests.cs

[Fact]
public void BAD_HighCoverage_ZeroProtection()
{
// This test RUNS every branch (coverage goes up)
// but does NOT verify the result. It passes even if the calculation is completely broken.
LoyaltyPointsCalculator.Calculate(50m, MemberTier.Standard, false);
LoyaltyPointsCalculator.Calculate(1_000m, MemberTier.Silver, false);
LoyaltyPointsCalculator.Calculate(1_000m, MemberTier.Gold, true);
LoyaltyPointsCalculator.Calculate(100_000m, MemberTier.Gold, true);

// Only assertion: "it did not blow up". In mutation testing, the number of mutants this test KILLS is about 0.
true.Should().BeTrue();
}

Walk through what the four calls reach:

  1. 50m takes the "below the threshold" branch and returns 0.
  2. 1_000m, Silver takes the Silver arm of the switch.
  3. 1_000m, Gold, true takes the Gold arm and the campaign branch.
  4. 100_000m, Gold, true reaches the cap.

Every branch of Calculate except the negative-amount guard runs, so line and branch coverage are close to 100%. Now break the code. Change the Silver multiplier to 1.20m, remove the campaign doubling, or swap Math.Min for Math.Max. This test still passes, because it never looks at the results. The only thing it can catch is an exception.

The assertion true.Should().BeTrue() makes it worse. It gives the test the shape of a real test, so a reviewer skimming the file sees an assertion and moves on.

The Same Coverage, Strong Protection

Here is a test that runs the same lines:

M06.BestPractices.Tests/CoverageAndMutationTests.cs

[Theory]
// The same lines, but the EXPECTED RESULT is written for each one.
// If the < in the production code becomes <=, the 100.00 row breaks: the mutant dies.
[InlineData(99.99, MemberTier.Standard, false, 0)]
[InlineData(100.00, MemberTier.Standard, false, 10)]
// If the Silver multiplier changes from 1.25 to 1.20, this row breaks: the mutant dies.
[InlineData(1_000, MemberTier.Silver, false, 125)]
// If the campaign multiplier changes from 2 to 3, this row breaks: the mutant dies.
[InlineData(1_000, MemberTier.Gold, true, 300)]
// If Math.Min becomes Math.Max, this row breaks: the mutant dies.
[InlineData(100_000, MemberTier.Gold, true, 5_000)]
public void GOOD_SameCoverage_StrongProtection(
decimal amountSpent, MemberTier tier, bool isCampaignDay, int expected)
{
LoyaltyPointsCalculator.Calculate(amountSpent, tier, isCampaignDay)
.Should().Be(expected);
}

The coverage report for these two tests looks almost the same. The difference is that each row pins one rule to an expected value. Every bug listed above turns at least one row red. The comments in the test name the exact change each row guards against; the next lesson turns that idea into a tool.

Showing these two tests next to each other is the clearest way to make the point: coverage is not a quality score. Two suites with the same percentage can differ completely in how many bugs they catch.

Boundaries Need Their Own Rows

Notice that the strong test has two rows around the threshold, 99.99 and 100.00, while the weak test has only 50. Coverage cannot tell these apart: 50 and 99.99 take the same branch. But only the second pair catches an off-by-one mistake in the comparison.

M06.BestPractices.Tests/CoverageAndMutationTests.cs

[Fact]
public void BoundaryTriplet_IsTheCheapestWayToKillOffByOneMutants()
{
// Boundary triplet: BELOW the threshold, EXACTLY AT it, ABOVE it.
// This is the cheapest way to catch "off-by-one" errors.
LoyaltyPointsCalculator.Calculate(99.99m, MemberTier.Standard, false).Should().Be(0);
LoyaltyPointsCalculator.Calculate(100m, MemberTier.Standard, false).Should().Be(10);
LoyaltyPointsCalculator.Calculate(100.01m, MemberTier.Standard, false).Should().Be(10);
}

The triplet costs three lines. It catches < changed to <=, the threshold constant changed to a nearby value, and a comparison flipped the wrong way. Use it for every limit in your rules: minimum spend, maximum points, expiry dates, age limits.

How to Use Coverage

Coverage is still useful, as long as you read it in the right direction:

  1. Low coverage is a reliable signal. A line that never runs in any test is certainly not protected. Use the report to find untested areas.
  2. High coverage is not a reliable signal. A line that runs might still be unverified. Do not treat 90% as proof of quality.
  3. Do not make coverage a hard target for individuals. When a number becomes the goal, people find the cheapest way to reach it, and assertion-free tests are the cheapest way.
  4. Review assertions, not percentages. In a code review, ask what each test checks and which bug it would catch.

A common mistake is to add a coverage gate to CI and consider the job done. The gate stops coverage from dropping, which has some value, but it says nothing about whether new tests verify anything. To measure that, you need mutation testing, which is the subject of the next lesson.

Summary

  1. Coverage tells you a line ran, not that its result was checked.
  2. A test with no real assertions can reach high coverage and still catch no bugs.
  3. Two suites with the same coverage can offer completely different protection.
  4. Add explicit rows around every boundary; the below/at/above triplet catches off-by-one errors.
  5. Use low coverage to find gaps, but do not read high coverage as proof of quality or make it an individual target.