Search code examples
unit-testingtestingcode-coveragemetricscode-metrics

What is the difference between block coverage and branch coverage?


Is block coverage the same as branch coverage, similar to it or completely different?

The top Google link explaining branch coverage: http://www.tutorialspoint.com/software_testing_dictionary/branch_testing.htm


Solution

  • Block coverage (or "basic block coverage") and branch coverage are two different measures of code coverage. Block coverage counts blocks bounded by branches. Branch coverage counts the actual branches.

    This code fragment

    puts "I'm block 1"
    if condition
      puts "I'm block 2"
    else
      puts "I'm block 3"
    end
    puts "I'm block 4"
    

    has four blocks but only two branches, the two sides of the if/else. If this code is tested by only one test and condition is true in that test,

    • blocks 1, 2 and 4 will be covered, so block coverage will be 75%
    • the true branch of the if will be covered but not the false branch of the if, so branch coverage will be 50%