# Claus Brabrand, ITU, Denmark Feb 17, 2009WHITE-BOX TESTING White-Box Testing Claus Brabrand [ ] ( “FÅP”: First-year Project Course, ITU,

## Presentation on theme: "Claus Brabrand, ITU, Denmark Feb 17, 2009WHITE-BOX TESTING White-Box Testing Claus Brabrand [ ] ( “FÅP”: First-year Project Course, ITU,"— Presentation transcript:

Claus Brabrand, ITU, Denmark Feb 17, 2009WHITE-BOX TESTING White-Box Testing Claus Brabrand [ brabrand@itu.dk ] ( “FÅP”: First-year Project Course, ITU, Denmark )

Claus Brabrand, ITU, Denmark Feb 17, 2009WHITE-BOX TESTING White-Box Testing Method

[ 3 ] Claus Brabrand, ITU, Denmark Feb 17, 2009WHITE-BOX TESTING Statement Coverage Testing if : TEST condition true and false if-else : TEST condition true and false while : TEST zero, one, more-than-one iterations in loop for : TEST zero, one, more-than-one iterations in loop

[ 4 ] Claus Brabrand, ITU, Denmark Feb 17, 2009WHITE-BOX TESTING White-Box Testing ”Coverage Table”: ”Expectancy Table”: Input property account > 90 account <= 90 Data set A B Choice 1 ife true false Data set A B   Input (98,3) (76,4) Expected output ”a=108, r=6” ”a= 86, r=2” Actual output ”a=108, r=6” ”a= 86, r=2” = void m(int account, int rate) { account = account + 10; if (account > 100) // 1 ife rate = rate * 2; else rate = rate / 2; out(“account = ” + account + “ rate = ” + rate); } Testing the world famous method: ”insert-\$10-then-double-rate-if-account- exceeds-\$100-otherwise-halve-rate”

[ 5 ] Claus Brabrand, ITU, Denmark Feb 17, 2009WHITE-BOX TESTING ”Coverage Table”: Coverage Table Input property No numbers At least one number Exactly one number Exactly two numbers At least three numbers N > current max N  current max N  cur max & N > cur min N  cur max & N  cur min Data set A B B C E C D E (3 rd num) E (2 nd num) Choice 1 ife true false 2 for zero-times once more-than-once 3 ife true false 4 if true false

[ 6 ] Claus Brabrand, ITU, Denmark Feb 17, 2009WHITE-BOX TESTING ”Expectancy Table”: Expectancy Table Data set A B C D E      Advice: Avoid expected 0’s (i.e., zeroes) (Default value in many languages.) Advice: Avoid reusing same numbers in tests (Data layout sometimes reuse old memory.) Input  [17] [27,29] [39,37] [49,47,48] Expected output ”no numbers” ”min=17,max=17” ”min=27,max=29” ”min=37,max=39” ”min=47,max=49” Actual output ”no numbers” ”min=17,max=17” ”min=27,max=29” ”min=39,max=39” ”min=49,max=49” =

[ 7 ] Claus Brabrand, ITU, Denmark Feb 17, 2009WHITE-BOX TESTING 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); }} Debugging ’ D ’ then reveals… /* 1 if-else */ /* 2 for */ /* 3 if-else */ /* 4 if */ if else for if else if Should have been: (obs < mi)

[ 8 ] Claus Brabrand, ITU, Denmark Feb 17, 2009WHITE-BOX TESTING Re-Test ! …as debugging often introduces new errors ! Fixed Program: Coverage Table: Expectancy Table:  Recall: no guarantee!

[ 9 ] Claus Brabrand, ITU, Denmark Feb 17, 2009WHITE-BOX TESTING Another Example public static void main ( String[] args ) { int mi1 = 0, mi2 = 0; if (args.length == 0) /* 1 if-else */ System.out.println("No numbers"); else { mi1 = Integer.parseInt(args[0]); if (args.length == 1) /* 2 if-else */ System.out.println("Smallest = " + mi1); else { int obs = Integer.parseInt(args[1]); if (obs < mi1) /* 3 if */ { mi2 = mi1; mi1 = obs; } for (int i = 2; i < args.length; i++) { /* 4 for */ obs = Integer.parseInt(args[i]); if (obs < mi1) /* 5 if-else */ { mi2 = mi1; mi1 = obs; } else if (obs < mi2) /* 6 if */ mi2 = obs; } System.out.println("The two smallest are: " + mi1 + " and " + mi2); } } }

[ 10 ] Claus Brabrand, ITU, Denmark Feb 17, 2009WHITE-BOX TESTING Coverage Table Choice Input property Data set 1 ife true No numbers 1 ife false At least one number 2 ife true Exactly one number 2 ife false At least two numbers 3 if true 2 nd number ≥ 1 st number 3 if false 2 nd number < 1 st number 4 for zero-times Exactly two numbers 4 for once Exactly three numbers 4 for more-than-once At least four numbers 5 ife true 3 rd number < current min 5 ife false 3 rd number ≥ current min 6 if true 3 rd ≥ cur min & 3 rd < 2 nd least 6 if false 3 rd ≥ cur min & 3 rd ≥ 2 nd least ABBCCDDEHEFFGABBCCDDEHEFFG

[ 11 ] Claus Brabrand, ITU, Denmark Feb 17, 2009WHITE-BOX TESTING Expectancy Table Data set Input Expected output = Actual output A  ”no numbers” ”no numbers” B [17] ”17” ”17” C [27,29] ”27 and 29” ”27 and 0” D [39,37] ”37 and 39” ”37 and 39” E [49,48,47] ”47 and 48” ”47 and 48” F [59,57,58] ”57 and 58” ”57 and 58” G [67,68,69] ”67 and 68” ”67 and 0” H [77,78,79,76] ”76 and 77” ”76 and 77”         Debugging reveals that variable ” mi2 ” erroneously retains initialization (0).

[ 12 ] Claus Brabrand, ITU, Denmark Feb 17, 2009WHITE-BOX TESTING Debugging public static void main ( String[] args ) { int mi1 = 0, mi2 = 0; if (args.length == 0) /* 1 if-else */ System.out.println("No numbers"); else { mi1 = Integer.parseInt(args[0]); if (args.length == 1) /* 2 if-else */ System.out.println("Smallest = " + mi1); else { int obs = Integer.parseInt(args[1]); if (obs < mi1) /* 3 if */ { mi2 = mi1; mi1 = obs; } for (int i = 2; i < args.length; i++) { /* 4 for */ obs = Integer.parseInt(args[i]); if (obs < mi1) /* 5 if-else */ { mi2 = mi1; mi1 = obs; } else if (obs < mi2) /* 6 if */ mi2 = obs; } System.out.println("The two smallest are: " + mi1 + " and " + mi2); } } } mi2 = obs; Re-Test: 

[ 13 ] Claus Brabrand, ITU, Denmark Feb 17, 2009WHITE-BOX TESTING Exercises Program (Factorial, iterative version): Exercise 1: Control-Flow Graph Exercise 2: White-Box Testing int factorial(int n) throws BadUserException { if (n<0) throw BadUserException; 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 Afleveringsopgave (i grupper) public List merge(List list1, List list2) { // invariant: we ASSUME list1 and list2 are sorted! 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; } Note: Do not forget to always re-test after debugging!

Download ppt "Claus Brabrand, ITU, Denmark Feb 17, 2009WHITE-BOX TESTING White-Box Testing Claus Brabrand [ ] ( “FÅP”: First-year Project Course, ITU,"

Similar presentations