liveBook · Manning

Code coverage

As explained in The Coder Cafe

Test coverage is a metric quantifying the portions, execution paths, behaviors, or cases of software exercised by tests.

Code coverage measures how much code a test suite executes. It usually means line coverage: the percentage of statements run during testing. The number is useful for tracking testing effort, but it does not say whether tests check the right result. Even 100% line coverage can leave important behavior or edge cases untested. section 5.1.2

Two measures reveal different gaps:

  • Line coverage asks whether each statement ran. It can be complete even when a test exercises only one outcome of a condition. section 5.1.3
  • Branch coverage asks whether tests took both the true and false outcomes of conditions, including those in loops. Use it alongside line coverage to spot paths worth testing. section 5.1.3
public int getNameLength(boolean isCoolUser) {
    User user = null;
    if (isCoolUser) {
        user = new John(); 
    }
    return user.getName().length(); 
}

A test with `isCoolUser` set to true executes every statement, giving 100% line coverage. With it set to false, `user` stays null and the return statement throws a null pointer exception. Branch coverage exposes the missing false outcome: the single test covers only 50% of branches. section 5.1.3

Track coverage, but resist treating a fixed percentage as a release gate. An 80% target can encourage tests written to satisfy the number rather than improve confidence. Review whether tests validate behavior, cover meaningful cases, and remain readable and maintainable. Green tests and a high coverage figure are not substitutes for those checks. section 5.1.2 section 5.6

Coverage also depends on which cases people thought to write. Property-based testing offers a complementary approach: define a general rule and generate many inputs that may reveal overlooked edge cases. For a proof of concept, keep enough tests to check correctness, even if testing effort is lighter; it may need to move into production sooner than expected. section 5.1.3 section 5.3.9

How The Coder Cafe covers it

How deep it goes
Defines line and branch coverage, works through a missed branch, and discusses percentage targets without prescribing a threshold.
Examples
Uses a conditional Java method where 100% line coverage misses a null pointer exception, plus an 80% coverage target.
What it assumes
Assumes familiarity with tests and conditional code while defining both coverage measures explicitly.
What you can do after
Diagnose/debug with line and branch coverage to identify untested outcomes without mistaking a high percentage for reliable tests.
How it connects
Connects coverage to unit tests, edge cases, property-based testing, and behavior-focused testing.

Also explained in

Compare how 22 books explain code coverage →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Coder Cafe. The section links point to the parts of the book it draws on.