Code Coverage: What Ran
Measure which parts of the design's code your tests ran: lines, branches, toggles and states. See why every hole matters, and why a perfect score still says nothing about what was never written.
You Will Be Able To
- Say what line, branch, toggle and state coverage each count.
- Read a coverage report and find the parts of the design no test reached.
- Explain why 100 percent code coverage is necessary and never sufficient.
- Name a bug that full code coverage cannot see: one in code that is missing.
- Lesson
- B3.5
- Time
- 20 min
- Runs on
- Your browser
- Check
- 5 questions
The coverage report is green from top to bottom. Every line of the counter ran, every branch went both ways, every bit rose and fell. Then a test presses up and down together, and the counter climbs when it should hold still.
- Design
- Result
- Control / FSM
- Data
- Count the lines
Every statement gets a counter. A line that never ran is a part of the design no test touched.
- Count the branches
Every if and else, and every choice of a case statement, gets its own counter.
- Count the toggles
Every bit of every signal: has it gone from 0 to 1, and from 1 to 0?
- Count the states
For a state machine, every state and every move between states.
- Know the limit
All four count code that exists. A line the designer forgot has no counter at all.
Read the diagram as text
- Design source: sat_counter, five lines of logic
- Line coverage: did each statement run?
- Branch coverage: did each if and else go both ways?
- Toggle coverage: did each bit rise and fall?
- State coverage: was each state of a state machine visited?
- What none of them see: code that is missing
- Design source → Line coverage: counts
- Design source → Branch coverage: counts
- Design source → Toggle coverage: counts
- Branch coverage → State coverage: for state machines
- State coverage → What none of them see: still blind to
An idea drawing. The counts in the lab come from a real coverage run, below.
Before You Start
Read Corner Cases and How to Think of Them first. A counter that stops at its ends is a refresher in Modulo-N, Up/Down and Loadable Counters.
| Question | |
|---|---|
| 1 | What do line, branch, toggle and state coverage count? |
| 2 | Who decides what code coverage measures: you or the tool? |
| 3 | What does a hole in code coverage tell you? |
| 4 | Why is 100 percent code coverage never enough? |
| 5 | Which kind of bug can no code count see? |
The Idea: A Map of What Ran
Every test you write runs some of the design's code and skips the rest. You cannot see which, by reading the tests. So simulators can count, for every line and branch of the design, how often it ran. The report is a map of the design coloured by use, and the pale parts are places no test has been.
This is code coverage. The tool builds it from the design's source, with no work from you. That is its great strength: it finds the forgotten corner automatically. If the reset branch never ran, or one bit of a register never rose, the report says so.
It is also its limit. Code coverage measures the design against itself, not against the specification. It can tell you a line ran. It cannot tell you the line is right, and it cannot count a line that should exist and does not.
New Words
| Word | Meaning |
|---|---|
| Code coverage | Counts, made by a tool from the design's source, of which parts of the code ran during the tests. |
| Line coverage | Whether each statement ran at least once. |
| Branch coverage | Whether each way out of every if and else was taken, and each choice of every case statement. |
| Toggle coverage | Whether each bit of each signal went from 0 to 1 and from 1 to 0. |
| State machine | Logic that is always in one of a few named states and moves between them on clock edges, like a traffic light. |
| State coverage | Whether each state of a state machine, and each move between states, was visited. |
| Hole | A part of the design that no test reached: a count of zero. |
The Smallest Example: Count the Branches by Hand
The design is sat_counter: it counts up or down and stops at 0 and at 15. The specification adds one rule: up and down together leave the count where it is. The test below counts up 17 times, down 17 times, then idles once. Which branches run?
| Branch | Runs when | Runs in this test? |
|---|---|---|
| rst | reset is high | yes, at the start |
| up and not at 15 | counting up | yes, 15 times |
| down and not at 0 | counting down | yes, 15 times |
| none (hold) | at the top, at the bottom, or idle | yes |
Every branch runs. Every bit of the count goes from 0 to 1 and back. Now look for the specification's rule about up and down together. Which line of the design handles it?
The Rules
Rule 1: Code coverage comes from the code.
The tool counts the design's own statements, branches and bits. You do not write it, and it knows nothing of the specification.
Rule 2: Every hole needs an answer.
A part of the design no test reached is untested. Write a test for it, or show it cannot happen.
Rule 3: 100 percent is necessary.
Code that never ran was never checked, so a project needs every hole answered.
Rule 4: 100 percent is never sufficient.
A line that ran may still be wrong, and a line that is missing has no counter at all.
Rule 5: Read coverage with checks.
Coverage says a line ran. Only a check says the result was right.
Sources for These Rules
Code coverage measures design structure and is derived by the tool. It does not cover the intent of the design: Mehta, section 7.1.
Line, branch and toggle points as reported by Verilator 5.052 (--coverage-line, --coverage-toggle, verilator_coverage --annotate).
The Lab: Every Line Ran, and the Bug Is Still There
First, a real coverage tool. The same counter and test ran on Verilator with line, branch and toggle coverage switched on. Here is the design's source with the count beside each line.
| Source line | Count | What ran |
|---|---|---|
| always_ff @(posedge clk) | 37 | every clock edge |
| if (rst) cnt <= 0; | 1 | reset |
| else if (up && cnt != 15) cnt <= cnt + 1; | 15 | counting up |
| else if (down && cnt != 0) cnt <= cnt - 1; | 15 taken, 6 not taken | counting down, or holding |
Each count is the tool's own. The lab's hand counters below see one more hold edge than this report. What matters in both is that no count is zero.
Every count is above zero. The toggle report has 16 points: clk, rst, up, down and the four bits of cnt, each rising and falling. Every one was hit. Rules 1 and 3 at work: the tool built this map from the code, and the map is complete.
The same test without the final idle cycle left one hole: down:1->0, with a count of 0. The test ended with down still high, so down never fell. That hole was real and cheap to close. Rule 2 at work.
Icarus, which runs the lab in this page, has no coverage tool. So the lab counts its branches by hand, with one counter per branch, the way a coverage tool would. It also checks every cycle against the specification:
branch rst ran 1 times
branch up and not at 15 ran 15 times
branch down and not at 0 ran 15 times
branch none of these (hold) ran 6 times
bits of cnt seen at 1: 1111, at 0: 1111
checks: 0 errorsFull coverage, zero errors. Now add three cycles of up and down together, and the checks report checks: 3 errors. Rule 4 at work: the design has no line for that rule, so up wins. No count anywhere in the report could have warned you.
Why a Measure with a Blind Spot Is Still Worth Having
Why use a measure that misses whole classes of bug? Because it costs almost nothing and finds what people forget. A team never decides to skip the error branch. It just never writes the test, and code coverage catches that every time.
The missing half comes from the specification, not from the code. Functional coverage, the next lesson, is written by you from the plan. It counts the situations that should happen, whether or not a line exists for them. A project needs both.
Traps
The first trap is treating the number as the goal. A team chases the last few percent with tests that run lines but check nothing. Coverage rises, and no bug is found. A test that runs a line without checking its result adds a count and no confidence.
The second trap is waving holes away. Some code truly cannot run, such as a branch for a value the inputs can never take. Mark it and say why, in writing, so the next reader can agree. A hole excused without a reason is often a missing test.
The third trap is the one in this lab: full coverage on a design that forgot a rule. No green report can see code that was never written. Only a check from the specification, driven by the right stimulus, finds it.
Where This Goes Next
Code coverage is the automatic half of measuring. The Coverage track teaches the half you write yourself.
| The idea from this page | Where it is taught |
|---|---|
| Counting the situations the plan asks for | Functional Coverage: What Was Tested |
| Code coverage beside functional coverage | What Coverage Measures |
| Closing holes in a coverage report | Closing Holes |
| Code that can never run, found by a formal tool | Unreachability and Coverage Closure |
| Counters that stop at their ends, in logic | Modulo-N, Up/Down and Loadable Counters |
Try It
Run it: the counter goes up past 15 and down past 0. A counter sits on each branch, and a check runs every cycle.
Add three cycles of up and down together before the quiet cycle: repeat (3) cycle_with(1, 1);. Predict the last line.
Which branch counter changes when you add those cycles, and does any counter stay at zero?
branch rst ran 1 times branch up and not at 15 ran 15 times branch down and not at 0 ran 15 times branch none of these (hold) ran 6 times bits of cnt seen at 1: 1111, at 0: 1111 checks: 0 errorsCheck Yourself
Common Mistakes
Wrong Take 100 percent code coverage as proof the design works.
Coverage counts what ran, not whether it was right, and cannot count missing code.
Fix Read coverage beside checks, and add functional coverage from the specification.
Wrong Add tests that run lines without checking anything.
The number rises and no bug can be found.
Fix Every test that closes a hole must also check the result.
Wrong Wave a hole away as unreachable without a reason.
Most such holes are missing tests.
Fix Write a test, or record in writing why the code cannot run.
Remember
- Code coverage counts which lines, branches, bits and states of the design ran.
- The tool builds it from the code with no work from you; every hole needs an answer.
- 100 percent is necessary and never sufficient: a line can run and be wrong.
- Missing code has no counter; only checks from the specification find it.