White Box Test: Cyclomatic Complexity

Daniel Solano
5 min readDec 17, 2019

--

The white box test is a test case design method that uses the procedural design control structure to derive the test cases.
White box tests checks that:

  • All independent paths of each module are executed at least once.
  • Decisions are used in their true part and in their false part.
  • All loops are executed at their limits.
  • All internal data structures are used.

Basis path testing

The basis path method (proposed by McCabe) makes it possible to obtain a measure of the complexity of a procedural design, and to use this measure as a guide for the definition of a series of basic paths of execution, designing test cases that guarantee that each path is run at least once.

Notation of the flow graph or program graph

Represents the logical control flow with the following notation:

Here is an example based on a flow chart representing the control structure of the program.

Cyclomatic complexity

It is a measure that provides an idea of ​​the logical complexity of a program.

  • Cyclomatic complexity coincides with the number of regions of the flow graph.
  • The cyclomatic complexity, V(G), of a flow graph G, is defined as V(G) = Edges Nodes + 2
  • The cyclomatic complexity, V(G), of a flow graph G, is also defined as V(G) = predicate nodes + 1

From the flow graph of the figure, the cyclomatic complexity would be:

  • Since the graph has four regions, V(G) = 4
  • Since the graph has 11 edges and 9 nodes, V(G) = 11 9 2 = 4
  • Since the graph has 3 predicate nodes, V(G) = 3 + 1 = 4

From the value of cyclomatic complexity we obtain the number of independent paths, which give us a limit value for the number of tests we have to design.
In the example, the number of independent roads is 4, and the independent roads are:

  • 1–11
  • 1–2–3–4–5–10–1–11
  • 1–2–3–6–7–9–10–1–11
  • 1–2–3–6–8–9–10–1–11

Test design steps through the basic path

  • Obtain the flow graph, from the design or module code.
  • Obtain the cyclomatic complexity of the flow graph.
  • Define the basic set of independent paths.
  • Determine the test cases that allow the execution of each of the previous paths.
  • Execute each test case and verify that the results are as expected.

Loop Testing

Loops are the cornerstone of the vast majority of algorithms implemented in software, so we have to pay special attention when testing the software.
The loop test is a white box test technique that focuses on the validity of loop constructions.

Four different types of loops can be defined:

  • Simple loops.
  • Nested loops.
  • Concatenated loops.
  • Unstructured loops.

Simple Loops

The following set of tests must be applied to simple loops (of n iterations):

  • Skip the loop.
  • Pass only once through the loop.
  • Go through the loop twice.
  • Make m loop steps with m <n
  • Do n 1, n and n + 1 steps through the loop.

Nested Loops

If we extended the set of tests from simple loops to nested loops, the number of tests would grow geometrically, so Beizer suggests the following set of tests for nested loops:

  • Start with the innermost loop, setting the other loops to the minimum values.
  • Carry out the simple loop tests for the innermost loop, keeping the iteration values ​​of the outermost loops at the minimum values.
  • Progress out on the next outermost loop, and keeping the outermost loops at their minimum values.
  • Continue until all loops have been tested.

Concatenated Loops

Test the concatenated loops using the test techniques for simple loops, considering them as independent loops.

Unstructured Loops

Redesign these loops to fit the constructions of structured programming.

Applying the theory

We’re going through a simple example of how this can be applied. Since is not just fair enough with the theory.

Check out this code below:

Given the code above, the specific flow graph for this program will be:

And mathematically speaking, the formula to be applied will be:

  • V(G) = 9 – 7 + 2 = 4
  • V(G) = 3 + 1 = 4 (Condition nodes are 1, 2 and 3 nodes)
  • Basis Set – A set of possible execution path of a program
  • 1-7
  • 1-2-6-1-7
  • 1-2-3-4-5-2-6-1-7
  • 1-2-3-5-2-6-1-7

Conclusion

This can help us maintain a secure and stable source code, avoiding leaks in loops or blocks of code that are not usable at the time of compilation or execution.

Is a useful software metric for structured or white-box testing. It is mainly used to assess the complexity of a program. If the decision points are more, then the complexity of the program is more. If the program has a high complexity number, then the probability of error is high with greater maintenance time and troubleshooting.

--

--

Daniel Solano

Senior Software Engineer with 7 years of experience in automation, QA and software development. Currently working for Blizzard Entertainment