0

I know that cyclomatic complexity defines the number of independent paths in the code. Using the simple way, it can be calculated as number of IF +1. In that case, for a simple code with two IFs, CC would be 3.

That would make sense in terms of TRUE/FALSE outcomes - TT, TF, FT. Each test case would traverse a new edge.

But what if I chose TT and FF? I would traversed all edges, how could there be another independent path?

EDIT: I might have understood...I assume linearly independent means that I change only one decisions outcome at time...

1

Cyclomatic complexity when applied to testing places two bounds on the test cases needed.

From Wikipedia Cyclomatic complexity: Implications for software testing

M is an upper bound for the number of test cases that are necessary to achieve a complete branch coverage.

M is a lower bound for the number of paths through the control flow graph (CFG). Assuming each test case takes one path, the number of cases needed to achieve path coverage is equal to the number of paths that can actually be taken. But some paths may be impossible, so although the number of paths through the CFG is clearly an upper bound on the number of test cases needed for path coverage, this latter number (of possible paths) is sometimes less than M.

All three of the above numbers may be equal: branch coverage <= cyclomatic complexity <= number of paths.

For the code:

if( c1() )
   f1();
else
   f2();

if( c2() )
   f3();
else
   f4();

This has a complexity of 3. Yes, testing TT or FF is two test cases and will cover the branch coverage of the code. The Npath coverage would require four tests.

A module should have at least as many tests as its complexity.

Consider a bug that requires that f1() be called before f3(). The tests TT and FF will not uncover the bug, while the tests TF and FT will show the bug. You should have at least 3 tests for this method according to cyclomatic complexity.

Though the key point in all of this is that the complexity establishes the bounds on the number of tests needed to adiquetly test the code. It does not say what tests you need (nor if you have the right tests).

  • Sure, I do understand that, I was just wondering about the fact that an independent path must cover an edge that was not covered before. When I use TT and FF, there is no path left. But I think that is what "linearly independent" means - I can change only one outcome while leaving the others the same. – user144171 Mar 10 '15 at 15:15
  • "Consider a bug that requires that f1() be called before f3()." TF will not expose that bug as f3() is never called. Only FT will expose this bug. – Sjoerd Jul 22 '15 at 3:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.