Claus Brabrand, ITU, Denmark Feb 17, 2009WHITE-BOX TESTING White-Box Testing Claus Brabrand [ ] ( “FÅP”: First-year Project Course, ITU, Denmark )
[ 2 ] Claus Brabrand, ITU, Denmark Feb 17, 2009WHITE-BOX TESTING successful test case unsuccessful test case Problematic terminology: successful test case unsuccessful test case Much better terminology: Psychology of Testing (cont’d) Goal: find as many errors as possible ”Testing is the process of executing a program with the intent of finding errors.” vs [cf. ”The Art of Software Testing” (Chap. 2), Glenford J. Myers, 1979]
[ 3 ] Claus Brabrand, ITU, Denmark Feb 17, 2009WHITE-BOX TESTING Testing vs. Debugging (cont’d) Evaluate test results Fix problem (reprogram) Functionality Testing Quality assurance: Perform (functionality) test Debugging Diagnosis: Determine problem? Program: (greater confidence!) SYSTEMATIC Document test results (confidence?!?) Re
[ 4 ] Claus Brabrand, ITU, Denmark Feb 17, 2009WHITE-BOX TESTING White-box vs. Black-box Test White-box Testing: (aka., ”structural testing”) (aka., ”internal testing”) Test focus: source code Black-box Testing: (aka., ”behavioral testing”) (aka., ”external testing”) (aka., ”input-ouput testing” ) Test focus: specification (or intention) Complementary Approaches!!! n = in(); n = n/2; odd(n) n = 3*n+1; out(n); tt ff ~ ? programspec
[ 5 ] Claus Brabrand, ITU, Denmark Feb 17, 2009WHITE-BOX TESTING Test Coverage? Method coverage: Does every method run (at least once)? Statement coverage: Does every statement run (at least once)? Branch coverage: Does every branch run (at least once)? Path coverage: Does every path run (at least once)?
[ 6 ] Claus Brabrand, ITU, Denmark Feb 17, 2009WHITE-BOX TESTING Statement coverage Statement coverage: Does every statement run (at least once)? -Box ”Statement Coverage Testing” is: Efficient (fast) ! Effective (thorough) ! Good for complicated program logic (esp. ”initialization errors”)
Claus Brabrand, ITU, Denmark Feb 17, 2009WHITE-BOX TESTING Control Structures & Control Flow Graphs
[ 8 ] Claus Brabrand, ITU, Denmark Feb 17, 2009WHITE-BOX TESTING Control Structures Control Structures: Statements (or Expr’s) that affect ”flow of control”: if-else : if : if ( Exp ) Stm 1 else Stm 2 if ( Exp ) Stm Stm 1 Exp truefalse Stm 2 confluence Stm Exp true false confluence The expression must be of type boolean; if it evaluates to true, the given statement is executed, otherwise not. The expression must be of type boolean; if it evaluates to true, Statement-1 is executed, otherwise Statement-2 is executed. [syntax] [semantics] [syntax] [semantics]
[ 9 ] Claus Brabrand, ITU, Denmark Feb 17, 2009WHITE-BOX TESTING Control Structures (cont’d) while : for : while ( Exp ) Stm for (Exp 1 ; Exp 2 ; Exp 3 ) Stm Equivalent to: The expression must be of type boolean; if it evaluates to false, the given statement is skipped, otherwise it is executed and afterwards the expression is evaluated again. If it is still true, the statement is executed again. This is continued until the expression evaluates to false. { Exp1; while ( Exp2 ) { Stm Exp3; } } Stm Exp true false confluence Exp 1 ; Exp 2 true false Stm confluence Exp 3 ; [syntax] [semantics] [syntax] [semantics]
[ 10 ] Claus Brabrand, ITU, Denmark Feb 17, 2009WHITE-BOX TESTING Example (Control-Flow Graph) Example Program: Control-Flow Graph: void m(int account, int rate) { account = account + 10; if (account > 100) rate = rate * 2; else rate = rate / 2; out(“account = ” + account + “ rate = ” + rate); } account = account + 10; rate *= 2; rate /= 2; account > 100 true false out(“account = ” + …); confluence
[ 11 ] Claus Brabrand, ITU, Denmark Feb 17, 2009WHITE-BOX TESTING Larger Example public static void main ( String[] args ) { int mi, ma; if (args.length == 0) System.out.println("No numbers"); else { mi = ma = Integer.parseInt(args[0]); for (int i=1; i < args.length; i++) { int obs = Integer.parseInt(args[i]); if (obs > ma) ma = obs; else if (mi < obs) mi = obs; } System.out.println("min=" + mi + "," + "max=" + ma); }} /* 1 if-else */ /* 2 for */ /* 3 if-else */ /* 4 if */ if else for if else if
[ 12 ] Claus Brabrand, ITU, Denmark Feb 17, 2009WHITE-BOX TESTING Control-Flow Graph CFG: int mi, ma; System.out.println ("No numbers"); args.length == 0 mi = ma = Integer.parseInt(args[0]); int i=1; i < args.length i++; System.out.println( " min=" + mi + "," + "max=" + ma); int obs = Integer.parseInt(args[i]); obs > ma ma = obs; mi < obs mi = obs; true false true false true false true false
[ 13 ] Claus Brabrand, ITU, Denmark Feb 17, 2009WHITE-BOX TESTING Exercises Program: Exercise 1: Control-Flow Graph Exercise 2: White-Box Testing int factorial(int n) { // iterative version if (n<0) System.out.println(“*** negative number!”); else { int res = 1; for (int i=1; i<=n; i++) { res = res * i; } return res; }
[ 14 ] Claus Brabrand, ITU, Denmark Feb 17, 2009WHITE-BOX TESTING Afl.Opg.: Control-Flow Graph public List merge(List list1, List list2) { ArrayList min; ArrayList max; if (list1.size() > list2.size()) { max = (ArrayList ) list1; min = (ArrayList ) list2; } else { max = (ArrayList ) list2; min = (ArrayList ) list1; } List list3 = new ArrayList (); for (int i=0; i < max.size(); i++) { for (int j=0; j < min.size(); j++) { if (max.get(i) > min.get(j)) { list3.add(min.get(j)); min.remove(j); } list3.add(max.get(i)); } if (min.size() > 0) { list3.addAll(min); } return list3; }
[ 15 ] Claus Brabrand, ITU, Denmark Feb 17, 2009WHITE-BOX TESTING Control Structures (cont’d 2 ) do-while : ” ?: ”; ”conditional expression”: ” && ”; ”lazy conjunction” (aka., ”short-cut ”): ” || ”; ”lazy disjunction” (aka., ”short-cut ”): switch : switch ( Exp ) { Swb * } case Exp : Stm * break; do Stm while ( Exp ); default : Stm * break; Swb: Exp 1 ? Exp 2 : Exp 3 Exp 1 && Exp 2 Exp 1 || Exp 2
[ 16 ] Claus Brabrand, ITU, Denmark Feb 17, 2009WHITE-BOX TESTING Control Structures (cont’d 3 ) try-catch-finally (exceptions): return / break / continue : ”method invocation”: e.g.; ”recursive method invocation”: e.g.; ”virtual dispatching”: e.g.; try Stm 1 catch ( Exp ) Stm 2 finally Stm 3 f(x) return ;return Exp ; break ;continue ; f(x)