structural testing
play

Structural Testing Maurcio Aniche M.F.Aniche@tudelft.nl - PowerPoint PPT Presentation

Structural Testing Maurcio Aniche M.F.Aniche@tudelft.nl SPECIFICATION Requirements Models Structure (e.g., source code) SPECIFICATION Requirements Models Structure (e.g., source code) public int public int play( int int left, int


  1. Structural Testing Maurício Aniche M.F.Aniche@tudelft.nl

  2. SPECIFICATION Requirements Models Structure (e.g., source code)

  3. SPECIFICATION Requirements Models Structure (e.g., source code)

  4. public int public int play( int int left, int int right) { Given the points of two int int ln = left; different players, the int rn = right; int program must return the if (ln > 21) if number of points the ln = 0; one who wins has! if if (rn > 21) rn = 0; if (ln > rn) if return rn; return else else return return ln; }

  5. public public int int play( int int left, What would you test? int int right) { (now, only looking to int int ln = left; the source code) int rn = right; int if (ln > 21) if ln = 0; if (rn > 21) if rn = 0; if (ln > rn) if return rn; return else else return return ln; }

  6. First idea: “going int play( int int left, public public int int right) { int through all the lines” int ln = left; int int rn = right; int If our test suite if (ln > 21) if ln = 0; exercises all the lines, if (rn > 21) if we are happy. rn = 0; if if (ln > rn) return rn; return else else return ln; return }

  7. First idea: “going int play( int int left, public public int int right) { int through all the lines” int ln = left; int int rn = right; int If our test suite if (ln > 21) if ln = 0; exercises all the lines, if (rn > 21) if we are happy. rn = 0; if if (ln > rn) return rn; return T1 = (30, 30) else else return ln; return } How many lines does it cover?

  8. First idea: “going int play( int int left, public public int int right) { int through all the lines” int ln = left; int int rn = right; int If our test suite if (ln > 21) if ln = 0; exercises all the lines, if (rn > 21) if we are happy. rn = 0; if if (ln > rn) return rn; return T1 = (30, 30) else else return ln; return }

  9. First idea: “going int play( int int left, public public int int right) { int through all the lines” 1 int int ln = left; int rn = right; 2 int If our test suite 3 if if (ln > 21) 4 ln = 0; exercises all the lines, 5 if if (rn > 21) we are happy. 6 rn = 0; 7 if if (ln > rn) 8 return return rn; T1 = (30, 30) 9 else else 10 return return ln; } 9 / 10 = 90% line coverage

  10. First criteria: “going int play( int int left, public public int int right) { int through all the lines” 1 int int ln = left; int rn = right; 2 int If our test suite 3 if if (ln > 21) 4 ln = 0; exercises all the lines, 5 if if (rn > 21) we are happy. 6 rn = 0; Make it true 7 if if (ln > rn) 8 return return rn; T1 = (30, 30) 9 else else T2 = (10,9) <-- left player wins 10 return return ln; }

  11. First criteria: “going int play( int int left, public public int int right) { int through all the lines” 1 int int ln = left; int rn = right; 2 int If our test suite 3 if if (ln > 21) 4 ln = 0; exercises all the lines, 5 if if (rn > 21) we are happy. 6 rn = 0; 7 if if (ln > rn) 8 return return rn; T1 = (30, 30) 9 else else T2 = (10,9) <-- left player wins 10 return return ln; } 10 / 10 = 100% line coverage

  12. Is this useful? int play( int int left, public public int int right) { int 1 int int ln = left; int rn = right; Yes, it is. We actually just found a bug ! 2 int 3 if if (ln > 21) 4 ln = 0; 5 if if (rn > 21) 6 rn = 0; 7 if if (ln > rn) 8 return return rn; 9 else else 10 return return ln; }

  13. int play( int int left, public public int Is this useful? int int right) { 1 int int ln = left; 2 int int rn = right; Yes, it is. We actually just found a bug ! 3 if if (ln > 21) 4 ln = 0; 5 if if (rn > 21) 6 rn = 0; 7 if if (ln > rn) return rn rn ; 8 return 9 else else return ln ln ; 10 return }

  14. int play( int int left, public public int Is this useful? int int right) { 1 int int ln = left; 2 int int rn = right; Yes, it is. We actually just found a bug ! 3 if if (ln > 21) 4 ln = 0; 5 if if (rn > 21) 6 rn = 0; 7 if if (ln > rn) return ln ln ; 8 return 9 else else return rn rn ; 10 return }

  15. Great! We found a bug after some structural testing !

  16. public public int int play( int int left, int int right) { 1. 1. int int ln = left; 2. int 2. int rn = right; 3. 3. if if (ln > 21) 4. 4. ln = 0; 5. 5. if if (rn > 21) 6. 6. rn = 0; 10 lines! 7. if 7. if (ln > rn) 8. 8. return return ln; 9. 9. else else 10. 10. return return rn; }

  17. public public int int play( int int left, int int right) { 1. 1. int int ln = left; 2. 2. int int rn = right; 3. 3. if if (ln > 21) ln = 0; 4. 4. if if (rn > 21) rn = 0; 5. 5. if if (ln > rn) return return ln; 6. 6. else return else return rn; 6 lines! }

  18. X 9/10 = 90%, 5/6 = 83%... From now on, I’ll write as many lines as I can!!

  19. How can I solve that…?

  20. Basic block ln = left ln = right • A basic block is a straight-line code sequence with no branches. ln > 21 • In other words, whenever you false true have a decision point, you start a new block. rn > 21 ln = 0 int int play( int int left, int int right) { true false int ln = left; int int rn = right; int rn = 0 if (ln > 21) if ln = 0; if (rn > 21) if ln > rn return rn return rn = 0; true if if (ln > rn) return rn; return false else else return ln; } return return return ln

  21. What’s the difference between line and statement coverage? • Line coverage looks at the lines of your program (as in the source code). • A line can contain more than one statement: – E.g., “a = 10; b=20;”

  22. Given a sentence, you should count the number of words that end with either an “s” or an “r”. A word ends when a non- letter appears.

  23. public public int int count(String str) { int int words = 0; char char last = ' ' ' ' ; for for ( int int i = 0;i<str.length(); i++) { if ( !Character. isLetter (str.charAt(i)) if && (last == 'r' 'r' || last == 's’ 's’ )) { What’s the words++; difference between } last = str.charAt(i); this program and } the other one if if (last == 'x' 'x' || last == 's’ 's’ ) words++; (when it comes to return return words; testing)? }

  24. Uhhh… there are so many if s and for s here! This program can take different paths!

  25. int words = 0; int We should cover Control-flow graph char char last = ' ' ' ' ; all the branches (CFG) (arrows) for for ( int int i = 0; i<str.length(); true false if (!Character. isLetter if if (last == ‘x’ if ‘x’ (str.charAt(i)) && || last == ‘s’ ‘s’ ) (last == ‘s’ ‘s’ || last == ‘r’ ‘r’ )) true true false words++; words++; false last = str.charAt(i); return words; return i++)

  26. Note on notation Decision blocks are often represented with diamonds . if if (!Character. isLetter (str.charAt(i)) && (last == ‘s’ ‘s’ || last (In here, I do not use it, because == ‘r’ ‘r’ )) they get too big and don’t fit an slide…) … …

  27. @Test public void public void multipleMatchingWords() { int int words = new new CountLetters() .count( "cats|dogs cats|dogs" ); Assertions. assertEquals (2, words); }

  28. int words = 0; int char char last = ' ' ' ' ; “cats|” for ( int for int i = 0; i<str.length(); true false if (!Character. isLetter if if if (last == ‘x’ ‘x’ (str.charAt(i)) && || last == ‘s’ ‘s’ ) (last == ‘s’ ‘s’ || last == ‘r’ ‘r’ )) true true false words++; words++; false last = str.charAt(i); return words; return i++)

  29. int words = 0; int char char last = ' ' ' ' ; “cats|dogs” for ( int for int i = 0; i<str.length(); true false if (!Character. isLetter if if if (last == ‘x’ ‘x’ (str.charAt(i)) && || last == ‘s’ ‘s’ ) (last == ‘s’ ‘s’ || last == ‘r’ ‘r’ )) true true false words++; words++; false last = str.charAt(i); return words; return i++)

  30. @Test public void public void lastWordDoesntMatch() { int int words = new new CountLetters() .count( "cats|dog cats|dog" ); Assertions. assertEquals (1, words); }

  31. int words = 0; int char char last = ' ' ' ' ; “cats|dog” for ( int for int i = 0; i<str.length(); true false if (!Character. isLetter if if if (last == ‘s’ ‘s’ (str.charAt(i)) && || last == ‘r’ ‘r’ ) (last == ‘s’ ‘s’ || last == ‘r’ ‘r’ )) true true false words++; words++; false last = str.charAt(i); return words; return i++)

  32. int words = 0; int char char last = ' ' ' ' ; “cats|dog” for ( int for int i = 0; i<str.length(); true false if (!Character. isLetter if if if (last == ‘s’ ‘s’ (str.charAt(i)) && || last == ‘r’ ‘r’ ) (last == ‘s’ ‘s’ || last == ‘r’ ‘r’ )) true true false words++; words++; false last = str.charAt(i); return words; return i++)

Recommend


More recommend