Structural Testing Maurício 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 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; }
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; }
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 }
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?
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 }
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
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; }
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
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; }
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 }
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 }
Great! We found a bug after some structural testing !
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; }
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! }
X 9/10 = 90%, 5/6 = 83%... From now on, I’ll write as many lines as I can!!
How can I solve that…?
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
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;”
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.
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)? }
Uhhh… there are so many if s and for s here! This program can take different paths!
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++)
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…) … …
@Test public void public void multipleMatchingWords() { int int words = new new CountLetters() .count( "cats|dogs cats|dogs" ); Assertions. assertEquals (2, words); }
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++)
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++)
@Test public void public void lastWordDoesntMatch() { int int words = new new CountLetters() .count( "cats|dog cats|dog" ); Assertions. assertEquals (1, words); }
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++)
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