Basic vs. compound condition coverage

后端 未结 2 1557
情话喂你
情话喂你 2021-01-16 12:56

I\'m trying to get my head around the differences between these 2 coverage criteria and I can\'t work out how they differ. I think I\'m failing to understand exactly what de

相关标签:
2条回答
  • 2021-01-16 13:29

    Regarding terminology, I don't have a single source handy that uses the exact terms "basic condition coverage" and "multiple condition coverage". Binder's "Testing Object-Oriented Systems" says "condition coverage" and "multiple-condition coverage". Everett & McLeod's "Software Testing" says "simple condition coverage" and "compound condition coverage". But I'm certain that the first term in each case is your "basic condition coverage" and the second is your "compound condition coverage". I'll use those terms below.

    Basic condition coverage means that every basic condition in the program is true in some test and false in some test, regardless of other conditions. In the following

    if a && b && c
      # do stuff
    else
      # do other stuff
    end
    

    there is a compound condition, a && b && c, with three basic conditions, a, b and c. It takes only two test cases, one where all basic conditions are true and one where all are false, to get full basic condition coverage. It doesn't matter that the basic conditions happen to be part of a compound condition.

    Note that basic condition coverage is not branch coverage. If the compound condition were a && b && !c, those two test cases above would still achieve basic condition coverage but would not achieve branch coverage.

    A less aggressively optimized set of test cases for basic condition coverage would have one test case where all three basic conditions are false and three test cases with a different basic condition true in each. That would still only be four of the eight possible combinations of basic conditions in the compound condition. The uncomfortable feeling that we're ignoring the other four is why there's compound condition coverage. That requires a test for each possible combination of basic conditions in a compound condition. In the example above, you'd need eight tests, one for each possible combination of possible values of a, b and c, to get full compound condition coverage.

    0 讨论(0)
  • 2021-01-16 13:34

    First, the difference between Decision and Condition.

    A Condition is an atomar boolean expression that can not be broken down into simpler boolean expression. For example: a (if a is boolean).

    A Decision is a compound of Conditions with zero or more Boolean operators. A Decision without an operator is also a condition. For example: (a or b) and c but also a and b or just a.

    Lets take a simple example

    if(decision)  {
      //branch 1
    } else {
      //branch 2
    }
    

    You need two tests to cover both branches. Thats the decision coverage or branch coverage. In case the decision is a condition (i.e. just a), that is also called basic condition coverage, which is the coverage of the two branches of a single condition.

    The decision can be broken down into conditions.

    Lets take for example

    decision = (a or b) and c
    

    The decision coverage would be achieved with

    • a,b,c = 0
    • a,b,c = 1

    But the permutation of all the combinations of its boolean sub expressions is the full condition coverage or multiple condition coverage), which is the compound of the basic condition coverage :

    | a | b | c |
    | 0 | 0 | 1 |
    | 0 | 0 | 0 |
    | 0 | 1 | 1 |
    | 0 | 1 | 0 |
    | 1 | 0 | 1 |
    | 1 | 0 | 0 |
    | 1 | 1 | 1 |
    | 1 | 1 | 0 |
    

    That would be quite a lot of tests, but some of those are redundant as some conditions are covered by others. This is reflected in the Modified Condition/Decision Coverage (MC/DC) which is a combination of condition coverage and function coverage.

    For MC/DC it is required, that each condition has to affect the outcome independently. With the above test (all are 0 or all are 1), we ignore the fact, that c-value doesn't matter if a and b are 0, or, that b-value doesnt matter if a and c are 1.

    So you should sit down, use your brain, and think about for which combinations the overall result R is 1 or 0.

      | a | b | c | a or b | c | R |  eq
    1 | 0 | 0 | 0 |    0   | 0 | 0 |  A
    2 | 0 | 0 | 1 |    0   | 1 | 0 |  B
    3 | 0 | 1 | 0 |    1   | 0 | 0 |  A
    4 | 0 | 1 | 1 |    1   | 1 | 1 |  C
    5 | 1 | 0 | 0 |    1   | 0 | 0 |  A
    6 | 1 | 0 | 1 |    1   | 1 | 1 |  D
    7 | 1 | 1 | 0 |    1   | 0 | 0 |  A
    8 | 1 | 1 | 1 |    1   | 1 | 1 |  D
    

    The last column shows the equivalence class:

    • A: c = 0, result is 0, neither a nor b have an influence
    • B: a,b = 0, result is 0, c has no influence
    • C: b,c = 1, result is 1, a has no influence
    • D: a,c = 1, result is 1, b has no influence

    For B and C it's quite obvious which to pick, not so for A and D. For each you have to check, what would happen, if I replace the operators, i.e. or -> and, and -> or, how will this affect the result of the (sub)decision. If the result will be affected, you got a candidate - If not, you don't.

    • A : (0 and/or 0) and/or 0 -> doesn't matter
    • A : (0 and 1) vs (0 or 1) -> does matter! -> Candidate
    • A : (1 and 0) vs (1 or 0) -> does matter! -> Candidate
    • A : (1 and/or 1) -> doesn't matter
    • D : (1 and 0) vs (1 or 0) -> does matter -> Candidate
    • D : (1 and 1) -> doesn't matter

    So you get the final test set as mentioned above:

    • a = 0, b = 1, c = 0 -> false branch (A) OR a = 1, b = 0, c = 0
    • a = 0, b = 0, c = 1 -> false branch (B)
    • a = 0, b = 1, c = 1 -> true branch (C)
    • a = 1, b = 0, c = 1 -> true branch (D)

    Especially the latter test - changing the operators - can be done with tools like mutation testing, that do not just replaing operators, but can do quite some more, i.e. flipping operands, removing statements, change order of execution, replace return values etc. And for each alteration of your code, it verifies if the test actually fails. This is good indicator of the quality of your test suite and ensures that code is not just covered but your tests for the code are actually valid.

    Regarding the terminology, I couldn't find the term "Compound Decision Coverage" somewhere. In my view a "compound decision" would be a compound of compounds of conditions, in other words: a compound of conditions.

    0 讨论(0)
提交回复
热议问题