Control Flow Testing
Control Flow Testing
Software Testing and QA Theory and Practice (Chapter 4: Control Flow Testing)
Basic Idea Outline of Control Flow Testing Control Flow Graph Paths in a Control Flow Graph Path Selection Criteria Generating Test Input Containing Infeasible Paths Summary
Software Testing and QA Theory and Practice (Chapter 4: Control Flow Testing)
Basic Idea
Control flow
Successive execution of program statements is viewed as flow of control. Conditional statements alter the default flow.
Program path
A program path is a sequence of statements from entry to exit. There can be a large number of paths in a program. There is an (input, expected output) pair for each path. Executing a path requires invoking the program unit with the right test input. Paths are chosen by using the concepts of path selection criteria.
Software Testing and QA Theory and Practice (Chapter 4: Control Flow Testing)
Figure 4.1: The process of generating test input data for control flow testing.
Software Testing and QA Theory and Practice (Chapter 4: Control Flow Testing) Naik & Tripathy
Selection of paths
Enough entry/exit paths are selected to satisfy path selection criteria.
Software Testing and QA Theory and Practice (Chapter 4: Control Flow Testing)
Symbols in a CFG
Software Testing and QA Theory and Practice (Chapter 4: Control Flow Testing)
Software Testing and QA Theory and Practice (Chapter 4: Control Flow Testing)
public static double ReturnAverage(int value[], int AS, int MIN, int MAX){ /* Function: ReturnAverage Computes the average of all those numbers in the input array in the positive range [MIN, MAX]. The maximum size of the array is AS. But, the array size could be smaller than AS in which case the end of input is represented by -999. */ int i, ti, tv, sum; double av; i = 0; ti = 0; tv = 0; sum = 0; while (ti < AS && value[i] != -999) { ti++; if (value[i] >= MIN && value[i] <= MAX) { tv++; sum = sum + value[i]; } i++; } if (tv > 0) av = (double)sum/tv; else av = (double) -999; return (av); }
10
11
Software Testing and QA Theory and Practice (Chapter 4: Control Flow Testing)
12
Program paths are selectively executed. Question: What paths do I select for testing? The concept of path selection criteria is used to answer the question. Advantages of selecting paths based on defined criteria:
Ensure that all program constructs are executed at least once. Repeated selection of the same path is avoided. One can easily identify what features have been tested and what not.
Software Testing and QA Theory and Practice (Chapter 4: Control Flow Testing)
13
All-path coverage criterion: Select all the paths in the program unit under consideration.
The openfiles() unit has 25+ paths.
Existence of file1 No No No No Yes Yes Yes Yes Existence of file2 No No Yes Yes No No Yes Yes Existence of file3 No Yes No Yes No Yes No Yes
Table 4.2: The input domain of openfiles() Selecting all the inputs will exercise all the program paths.
Software Testing and QA Theory and Practice (Chapter 4: Control Flow Testing) Naik & Tripathy
14
Software Testing and QA Theory and Practice (Chapter 4: Control Flow Testing)
15
SCPath1 SCPath2
1-2-3(F)-10(F)-11-13 1-2-3(T)-4(T)-5-6(T)-7(T)-8-9-3(F)-10(T)-12-13
Table 4.4: Paths for statement coverage of the CFG of Figure 4.7.
Software Testing and QA Theory and Practice (Chapter 4: Control Flow Testing)
16
Software Testing and QA Theory and Practice (Chapter 4: Control Flow Testing)
17
Figure 4.8: The dotted arrows represent the branches not covered by the statement covering in Table 4.4.
Software Testing and QA Theory and Practice (Chapter 4: Control Flow Testing) Naik & Tripathy
18
Table 4.5: Paths for branch coverage of the flow graph of Figure 4.7.
Software Testing and QA Theory and Practice (Chapter 4: Control Flow Testing)
19
Software Testing and QA Theory and Practice (Chapter 4: Control Flow Testing)
20
Figure 4.9: Partial control flow graph with (a) OR operation and (b) AND operation.
Software Testing and QA Theory and Practice (Chapter 4: Control Flow Testing) Naik & Tripathy
21
Having identified a path, a key question is how to make the path execute, if possible.
Generate input data that satisfy all the conditions on the path.
Software Testing and QA Theory and Practice (Chapter 4: Control Flow Testing)
22
Input vector
An input vector is a collection of all data entities read by the routine whose values must be fixed prior to entering the routine. Members of an input vector can be as follows. Input arguments to the routine Global variables and constants Files Contents of registers (in Assembly language programming) Network connections Timers Example: An input vector for openfiles() consists of individual presence or absence of the files files1, file2, and file3. Example: The input vector of ReturnAverega() shown in Figure 4.6 is <value[], AS, MIN, MAX>.
Software Testing and QA Theory and Practice (Chapter 4: Control Flow Testing)
23
Predicate
A predicate is a logical function evaluated at a decision point. Example: ti < AS is a predicate in node 3 of Figure 4.7. Example: The construct OB is a predicate in node 5 in Figure 4.9.
Path predicate
A path predicate is the set of predicates associated with a path. Figure 4.10: An example path from Fig. 4.7: 1-2-3(T)-4(T)-5-6(T)-7(T)-8-9-3(F)-10(T)-12-13. Figure 4.11: The path predicate for the path shown in Figure 4.10.
ti < AS True value[i] != -999 True value[i] >= MIN True value[i] <= MAX True ti < AS False tv > 0 True
Software Testing and QA Theory and Practice (Chapter 4: Control Flow Testing) Naik & Tripathy
24
Predicate interpretation
A path predicate may contain local variables. Example: <i, ti, tv> in Figure 4.11 are local variables. Local variables play no role in selecting inputs that force a path to execute. Local variables can be eliminated by a process called symbolic execution. Predicate interpretation is defined as the process of
symbolically substituting operations along a path in order to express the predicate solely in terms of the input vector and a constant vector.
A predicate may have different interpretations depending on how control reaches the predicate.
Software Testing and QA Theory and Practice (Chapter 4: Control Flow Testing)
25
0 < AS True (1) value[0] != -999 True (2) value[0] >= MIN True (3) value[0] <= MAX True (4) 1 < AS False (5) 1>0 True (6)
Software Testing and QA Theory and Practice (Chapter 4: Control Flow Testing) Naik & Tripathy
26
0 < AS True (1) value[0] != -999 True (2) 0>0 True (3)
Software Testing and QA Theory and Practice (Chapter 4: Control Flow Testing)
27
One can solve the above equations to obtain the following test input data
AS MIN MAX Value[0] = = = = 1 25 35 30
28
Software Testing and QA Theory and Practice (Chapter 4: Control Flow Testing)
29
Summary
Control flow is a fundamental concept in program execution. A program path is an instance of execution of a program unit. Select a set of paths by considering path selection criteria.
Statement coverage Branch coverage Predicate coverage All paths
From source code, derive a CFG (compilers are modified for this.) Select paths from a CFG based on path selection criteria. Extract path predicates from each path. Solve the path predicate expression to generate test input data. There are two kinds of paths.
feasible infeasible
Software Testing and QA Theory and Practice (Chapter 4: Control Flow Testing) Naik & Tripathy
30