Condition coverage for If(A && B) – measuring the input or code exercised

conditionstest-coveragetesting

I have been studying condition coverage for last few days. In a book "The Art of Software Testing" they highlight that it does not assure decision coverage, because for example

IF(A && B)

A=True, B=false and A=False,B=True satisfies the condition coverage but the decision is always False.

I wonder, how can this satisfy the coverage? The True value of B will actually never be tested because of short circuiting so how it can satisfy the coverage?

Best Answer

Condition coverage is about testing that any operands which are part of the if expression can be evaluated to both true and false without issues. In this type of coverage, you don't care about what would be executed inside the braces, nor about the result of the operand used in a condition.

Take the following code:

bool a = ...;
bool b = ...;
if (a && b)
{
    ...
}

Condition coverage of this code would require to test:

  • a for both true and false, and:
  • b for both true and false.

In languages which require both operands to evaluate in a logical operation, condition coverage of the code above is satisfied with:

a = true; b = false
a = false; b = true

This is what is illustrated in your question.

Some languages evaluate the second operand of a logical AND only if the first operand is true (or evaluate the second operand of OR only if the first operand evaluates to false). In this case, three tests are required instead of two:

a = true; b = true
a = true; b = false
a = false

As you can note, we don't care about the value of b in the third test, since the program will never compute it when a is false.

Related Topic