Definition
Basic Block Coverage
Basic Block Coverage is a structural testing metric that measures the percentage of basic blocks in the Control Flow Graph that have been executed at least once.
Calculation Note: When counting the “Total number of blocks,” we exclude the special Entry and Exit blocks. We only count the blocks containing actual code statements.
Equivalence to Statement Coverage
In this course (and many tools), Basic Block Coverage is used as the practical definition of Statement Coverage.
- Reasoning: A basic block is a sequence of instructions with a single entry and single exit. If the block is entered, all statements within it are executed. Therefore, covering all blocks implies covering all statements.
- Difference: The calculation denominator differs (Blocks vs. Statements vs. Lines), but the coverage goal (executing the code) is identical.
Example
Consider the absoluteSum method:
graph TD Entry((Entry)) --> B1["BB<sub>1</sub>: sum = 0"] B1 --> b1{"BB<sub>2</sub>: b<sub>1</sub> = x < 0"} b1 -- "True" --> B3["BB<sub>3</sub>: x = -x"] b1 -- "False" --> b2{"BB<sub>4</sub>: b<sub>2</sub> = y < 0"} B3 --> b2 b2 -- "True" --> B5["BB<sub>5</sub>: y = -y"] b2 -- "False" --> B6["BB<sub>6</sub>: sum = x + y"] B5 --> B6 B6 --> Exit((Exit))
To achieve 100% Basic Block Coverage, we must execute blocks BB_1 through BB_6.
- Total Blocks: 6 (exclude Entry and Exit).
- Test Case:
- Path: Entry Exit
- Result: 6/6 Blocks Executed (100%).