Cyclomatic Complexity for Software Testing (Part 2)

In the previous article we discussed about what is Cyclomatic Complexity, how to calculate it and the interpretation of the Cyclomatic Complexity number.

Now let’s discuss how Cyclomatic Complexity metric is useful for software testing.

How Cyclomatic Complexity metric is useful for Software Testing

Cyclomatic Complexity Measure indicates the maximum number of test cases that must be generated to achieve branch coverage. For this type of test coverage, a basis set of paths constitutes a set of test cases that ensures branch coverage.

Basis Set- A set of possible execution path of a program

Let’s see the Basis Set for the previous example in the previous article.

The example program and the flow graph were as follow;

Example Program-Screenshot_4

Flow graph-
Screenshot_2

There are 4 execution paths available in the above example.

  • 1, 2, 3, 4, 5, 7
  • 1, 2, 6, 7
  • 1, 2, 3, 6, 7
  • 1, 2, 3, 4, 6, 7

Basis Path testing is one of White box technique and it guarantees to execute at least one statement during testing. It checks each linearly independent path through the program, which means number of test cases, will be equivalent to the cyclomatic complexity of the program.

Cyclomatic Complexity values can therefore be used to identify which areas of the software will require greater testing effort than others. The lower the Cyclomatic Complexity, the easier it is to determine suitable tests and to perform them.

This metric is useful because of properties of Cyclomatic complexity (M) –

  1. M can be number of test cases to achieve branch coverage
  2. M can be number of paths through the graphs

For the above example, four test cases are necessary to the complete path coverage as complexity has calculated as 4.

Research has shown that minimizing complexity is a key to writing high quality code. If the Cyclomatic Complexity of software is measured regularly during construction then complex areas of code can be identified early in the development life cycle and re-structured to improve understanding and to reduce testing and future maintenance effort.

Tools for Cyclomatic Complexity Calculation

Cyclomatic complexity can be calculated manually if the program is small. Automated tools need to be used if the program is very complex as this involves more flow graphs.

Many tools are available for determining the complexity of the application. Some complexity calculation tools are used for specific technologies. As mentioned in the previous article, Complexity can be found by the number of decision points in a program. The decision points are if, for, for-each, while, do, catch, case statements in a source code.

Examples of tools are;

  • OCLint – Static code analyzer for C and Related Languages
  • devMetrics – Analyzing metrics for C# projects
  • Reflector Add In – Code metrics for .NET assemblies
  • GMetrics – Find metrics in Java related applications
  • NDepends – Metrics in Java applications

Uses of Cyclomatic Complexity

  • Helps developers and testers to determine independent path executions
  • Developers can assure that all the paths have been tested at least once
  • Helps us to focus more on the uncovered paths
  • Improve code coverage
  • Evaluate the risk associated with the application or program
  • Using these metrics early in the cycle reduces more risk of the program
  • Allows developers to identify modules that are difficult to test or maintain. Developers can use this measure to determine which modules of a program are overly-complex and need to be re-coded

Conclusion

Cyclomatic Complexity is software metric useful for structured or white box testing .It is mainly used to evaluate complexity of a program. If the decision points are more, then complexity of the program is more. If program has high complexity number, then probability of error is high with increased time for maintenance and trouble shoot.

The Cyclomatic Complexity of a procedure can also be used as an indicator of the possible risk associated with modifying a procedure. Higher complexity will normally result in higher cost and risk when making modifications.

 

Leave a Reply

Your email address will not be published. Required fields are marked *