Search code examples
branchcode-coveragecode-analysiscontrol-flow

Unconditional branching and code coverage


So I have learned that branch coverage differs from decision coverage as branch coverage typically includes also unconditional branches, e.g. methods calls, using of throw, break and other keywords in C#. But I wonder, is this kind of branch coverage actually used in code analyzers? I suspect they use decision coverage, making sure that all decision outcomes (i.e. resulting branches) are covered. I mean, the following code has 2 conditional, but 5 unconditional branches:

if(A)
  B();
  C();
  D();
  E();
else
 X();

And I believe that if I write a test to evaluate A to just false, the code analyzers will tell me that the branch coverage is 50%. But from the unconditional branches perspective, more will nto be executed. Is that correct?


Solution

  • Branch coverage doesn't tell you if a decision has been tested as both true and false.

    Example:

     if (c) {
        x=...
     }
     y=...
    

    If c evaluates to TRUE, the block containing x=... is executed, and branch coverage will detect that. It will also detect that the code starting at y has been executed. So you'll get 100% coverage if C is true, without having any idea what happens if C is false.

    With decision coverage, you would know that C has been evaluated and produces both TRUE and FALSE, if you had 100% coverage.

    If your conditional if has a then block and an else block, then branch coverage and decision coverage will give you the same information.