We defined the terms White Box and Black Box testing in Lecture #3. We will now discuss techniques for these testing strategies. We start with a widely known White Box technique known as Basis Path testing.
On page 178 of Chapter 9, the authors introduce the term Cyclomatic Complexity (or Cyclomatic Number). The Basis Path testing technique is based on this notion. The technique therefore requires that the module/program being tested must first be expressed as a directed graph. These directed graphs are often referred to as Program Graphs. The Cyclomatic Complexity/Number may then be determined and indicates the number of independent pathways through the program. These pathways may then be used to derive the minimum set of test cases to ensure statement coverage (but only weak decision coverage).
Following is some basic terminology followed by an outline of the technique.
Terminology:
Cyclomatic Complexity/Number:
On page 179 the authors present three different expressions that may be used to compute Cyclomatic Number. We will use the following:
where E represents the number of edges of the program graph and N represents the number of nodes.
Basis Path Testing:
Recall, that the idea of the technique, is to determine a minimum set of test cases that will ensure statement coverage and possibly also decision coverage. Remember that weak decision coverage may be obtained if selection structures are restructured as basic clauses. That is, predicates consisting of conjunctions (i.e. AND) may be restructured as nested selection structures.
For a given program, the technique may be described thus:
Example:
Consider the following pseudocode that iterates through a list identifying
numeric and non-numeric characters and then prints a count of the
items in the list:
Note: Numbers at the left correspond to nodes in the
program graph.
1 count=0 1 get first item in list. 2 do while not end of list 3 count=count + 1 4 if item numeric 5 print 'NUMERIC', item else 6 print 'NON-NUMERIC', item 7 endif 8 get next item in list. 9 enddo 10 print countSince the program graph contains 10 nodes and 11 edges then the cyclomatic number for the graph is:
Therefore, the corresponding minimum test set should ensure that the following three situations are addressed:
In Class Exercise:
Consider the following pseudocode that
determines the total price for an item purchased from an electronic retailer.
Note: Numbers at the left correspond to nodes in the
program graph.
1 rushCharge = 0 1 if nextday = 'Y' 2 rushCharge = 14.50 3 endif 3 tax = amount * 0.08 3 if amount >= 1000 4 shipcharge = amount * .06 + rushCharge 5 else if amount >= 200 6 shipcharge = amount * .08 + rushCharge 7 else if amount >= 100 8 shipcharge = 10 + rushCharge 9 else if amount >= 50 10 shipcharge = 9 + rushCharge 11 else if amount >= 25 12 shipcharge = 7 + rushCharge else 13 shipcharge = 5 + rushCharge 14 endif 14 total = amount + tax + shipchargeNotice that in this case my program graph is made more compact by, for example, collapsing the node for the first statement into that for the the first if statement. Similarly, node 3 could have been expressed as three distinct nodes. In any case, the resulting cyclomatic number will be the same.
Since the program graph contains 14 nodes and 19 edges then the cyclomatic number for the graph is:
Therefore, the corresponding minimum test set should ensure that the following seven situations are addressed: