Statement and Branch Coverage

Coverage indicates which code ran during tests. Branch coverage checks whether the alternatives of a decision were traversed.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Find a missing alternative
  2. Keep meaningful assertions
  3. Observe how one executed branch leaves another untested
  4. Check behavior, not only reachability

Find a missing alternative

An if statement increments a count for a qualifying item. A test with only qualifying items can execute every statement in that function. The false branch still needs a case. Truth tables can expose more independent conditions inside the test.

Keep meaningful assertions

Executing both branches leaves correctness unanswered if no result is checked. Specify the promised result for a qualifying and an unqualified item and inspect the resulting state. Function postconditions guide those checks.[1]

Observe how one executed branch leaves another untested

A function starts with a count of zero, increments inside an if when a flag is true and then returns. With true input, every statement executes. The false exit from the condition remains untested. Use a false input to exercise that distinct behavior. See decision tables.

Case flag=true: increment path, result 1
Case flag=false: skip path, result 0
One true case can execute every statement while missing a branch

Check behavior, not only reachability

Coverage reports where code ran. It does not show whether the test made an assertion capable of detecting a wrong output. Add observed expected results and choose an input that distinguishes competing behaviors.

References

  1. Python Software Foundation, unittest: Unit Testing Framework ↩