software-engineering testing

Definition

Statement Coverage

Statement coverage (or ) is a structural testing metric that measures the percentage of executable statements (or basic blocks) in the source code that have been executed at least once.

Concept

In the context of a Control Flow Graph, statement coverage requires visiting every node (Basic Block) in the graph.

  • Goal: Ensure every line of code is run at least once.
  • Limitation: It is the weakest form of coverage. It does not guarantee that all decisions (branches) are tested, nor does it check logical combinations.

Subsumption

Statement coverage is subsumed by stronger criteria:

  • Branch Coverage Statement Coverage: If you achieve 100% Branch Coverage, you effectively achieve 100% Statement Coverage.
  • The Reverse is False: 100% Statement Coverage does not imply 100% Branch Coverage (as shown in the example below).

Example

Consider the absoluteSum method and its CFG:

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% Statement Coverage, we need to visit every Basic Block (BB_1 to ).

Test Case 1: x = -1, y = -1

  • Path:
  • Nodes Visited: (100% Coverage)

Analysis: A single test case is sufficient to execute every statement. However, this test case never validates the False branches (where or ). If there were a bug on the “False” path (e.g., a missing else logic), this metric would not detect it.