Anda di halaman 1dari 9

CODE COMPLEXITY TESTING

Structural Testing
Code Complexity Testing
 Two questions may arise while using coverage:
1. Which of the paths are independent? If two paths are not
independent, then we may be able to minimize the number of tests.
2. Is there an upper bound on the number of tests that must be
run to ensure that all the statements have been executed at least
once?
 Cyclomatic complexity is a metric that quantifies the complexity of
a program and provides answers to the above questions.
Cyclomatic Complexity = Number of predicate nodes + 1
Cyclomatic Complexity = E - N + 2
Structural Testing
Code Complexity Testing
 A program is represented in the form of a flow graph. A flow
graph consists of nodes and edges.
 In order to convert a standard flow chart into a flow graph to
compute cyclomatic complexity, the following steps can be taken.
1. Identify the predicates or decision points in the program.
2. Ensure that the predicates are simple (i.e., no and/or).
If there are loop constructs,
break the loop
termination checks
into simple predicates.
Structural Testing
Code Complexity Testing
3. Combine all sequential statements into a single node.
4. When a set of sequential statements are followed by a simple
predicate, combine all the sequential statements and the predicate
check into one node and have two edges starting from this one
node. Such nodes with two edges starting from them are called
predicate nodes.
5. Make sure that all the edges terminate at some node.
Structural Testing
Code Complexity Testing
Structural Testing
Code Complexity Testing
 no decision points.
 has two nodes, one for the code and
one for the termination node.
 only one edge, and is the only
independent path. Hence, for this
flow graph, cyclomatic complexity is
equal to one.
 no predicate nodes because there are no decision points.
Hence, the cyclomatic complexity = 0 (the number of predicate
nodes) + 1.
 the edges (E) = 1; nodes (N) = 2. The cyclomatic complexity = E - N
+ 2 => l + 2 - 2 =>1
Structural Testing
Code Complexity Testing
 two independent paths, and thus,
the cyclomatic complexity of the
graph equals 2 [ the number of
predicate nodes (1) + 1].
 E = 4, N= 4, So, E – N + 2 => 4-4+2 =
2.
 An independent path can be defined
as a path in the flow graph that has at
least one edge that has not been
traversed before in other paths.
Structural Testing
Code Complexity Testing
 A set of independent paths that cover all the edges is a basis set.
 Once the basis set is formed, test cases should be written to
execute all the paths in the basis set.

Complexity What it means?

Code Complexity Testability Cost/effort to maintain

1 – 10 Well-written High Low

10 – 20 Moderate Medium Medium

20 – 40 Very complex Low High

>40 Not testable Cost/effort may not be


enough
Structural Testing
Code Complexity Testing
Calculating and using cyclomatic complexity
 For small programs cyclomatic complexity can be calculated
manually.
 Tools are used to calculate cyclomatic complexity for thousands
of lines of code.

Challenges in White-box Testing


 Human tendency of a developer being unable to find the
defects in his code
 Fully tested code may not correspond to realistic scenarios

Anda mungkin juga menyukai