user5182503
user5182503

Reputation:

Cyclomatic complexity and basis path

Lets, consider we have the following method:

public void testIt(boolean a, boolean b){
   if (a && b){
   ...
   }
   if (a){
   ....
   }
}

Cyclomatic complexity of this method =3. So, according to basis path testing we should have 3 tests in order to achieve statement and decision coverage. However, I see that I can use only two tests (true, true) and (false,false) to achieve statement and decision coverage. Where is my mistake?

Upvotes: 1

Views: 735

Answers (2)

Old Fox
Old Fox

Reputation: 8725

Yes, you are right. The Cyclomatic complexity is 3 and the cases you should verify are:

  1. a is false, b - we don't care (I explain later...) -> nothing happened

  2. a is true, b is false -> only the second condition was executed

  3. a and b are true -> both the first and the second conditions were executed

If you look only through the arguments the first option I mentioned has 2 different inputs(b true/false), however in both cases the same should happen, so I offer you to verify it only once or to use the equivalent to C# test case attribute

Upvotes: 6

Iłya Bursov
Iłya Bursov

Reputation: 24146

answer from @OldFox is correct, just some additions:

Where is my mistake?

CC is upper bound for branch coverage, but it is lower bound for paths coverage (not the same as line/statement coverage, see below). So you need up to 3 tests to cover all branches/conditions and at least 3 tests to cover all paths.

Here is the graph of your function:

CFG

CC = 6 - 5 + 1*2 = 3 according to definition

To cover all branches you need up to 3 tests, and actually you need all 3 to cover (true, true), (true, false), and (false, *)

To cover all paths you need at least 3 tests, there are 3 possible independent paths in graph, so you need only 3 tests to cover all paths.

There could be some confusion about number of different inputs, which is 4, but not all paths formed by these inputs are independent, two of them (when a is false) are actually the same, so there are only 3 independent paths

conclusion, 3 test cases are necessary and sufficient to provide both branch and paths coverage for your function.

now about line/statement coverage: you need to execute every line at least ones, to achieve this you need only one test (true, true), but clearly it is not enough for other types of coverage.

Upvotes: 1

Related Questions