Whitebox Testing http flic krpohtmj SWEBOK Knowledge Areas
Whitebox Testing http: //flic. kr/p/ohtmj
SWEBOK Knowledge Areas 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. Software Requirements Software Design Software Construction Software Testing Today’s topic Software Maintenance Software Configuration Management Software Engineering Process Software Engineering Models and Methods Software Quality Software Engineering Professional Practice Software Engineering Economics Computing Foundations Mathematical Foundations Engineering Foundations
Recall: Common approaches for choosing test cases • Blackbox testing: Choose based on module’s possible inputs and outputs – Do not use code – Often test boundary cases • White-box testing: Uses internal logic to choose tests – Different levels of code coverage – Aka glass box testing, clear box testing • Regression testing: Keep tests that reveal old bugs – Rationale: “Fixed” bugs come back!
Criteria for choosing test cases: Coverage Measures • Degree to which the source code of a program is tested by a test suite • Examples: – Statement coverage – Condition coverage – Path coverage Some examples will clarify, but first…
Control Flow Graphs – Frequently used to calculate coverage Control Flow Graph int foo(int x, int y) { int z = 0; if ((x>0) && (y>0)) { z = x; } return z; } int z = 0; if ((x>0) && (y>0)) { Basic blocks: straight-line pieces of code without any jumps or jump targets return z; true z = x; false Jumps: control branches
Statement Coverage • Set of test cases such that… Each program statement (line or basic block) is executed at least once
Define a test suite that provides statement coverage for this code Control Flow Graph int foo(int x, int y) { int z = 0; if ((x>0) && (y>0)) { z = x; } return z; } input x 1 y 1 expected 1 ✔ int z = 0; if ((x>0) && (y>0)) { true ✔ z = x; ✔ return false z;
Now try this code… Control Flow Graph int result=0; result = x*2; x < 69 T F x = 6969; x % 2 == 0 T F x > 6969 F public int bar(int x) { int result=0; if (x < 69) { if (x % 2 == 0) { result = x/2; } else { result = x*2; } } else if (x > 6969) { x = 69; } else { x = 6969; } return result; } result = x/2; T x = 69; return result;
Now try this code… ✔int result=0; < 69 T > 6969 F = 6969; ✔ result ✔x 6970 70 69 6969 T ✔x = x*2; F % 2 == 0 ✔result F ✔x expected 2 1 Control Flow Graph ✔x ✔x Input (x) 1 2 T = x/2; = 69; ✔ return result;
Condition Coverage • Set of test cases such that… Each boolean expression (in control structures) evaluates to true at least once and to false at least once
Define a test suite that provides condition coverage for this code Control Flow Graph int foo(int x, int y) { int z = 0; if ((x>0) && (y>0)) { z = x; } return z; } input x 1 0 y 1 0 expected 1 0 int z = 0; if ((x>0) && (y>0)) { ✔true z = x; return z; ✔false
Now try this code… Control Flow Graph int result=0; result = x*2; x < 69 T F x = 6969; x % 2 == 0 T F x > 6969 F public int bar(int x) { int result=0; if (x < 69) { if (x % 2 == 0) { result = x/2; } else { result = x*2; } } else if (x > 6969) { x = 69; } else { x = 6969; } return result; } result = x/2; T x = 69; return result;
Now try this code… Control Flow Graph Input (x) 1 2 6970 70 int result=0; result = x*2; x < 69 ✔ x = 6969; x % 2 == 0 ✔ ✔ F F T F x > 6969 ✔ ✔ T result = x/2; ✔x T = 69; return result; expected 2 1 69 6969
Path Coverage • Set of test cases such that… Each possible path through a program’s control flow graph is taken at least once
Define a test suite that provides path coverage for this code Control Flow Graph int foo(int x, int y) { int z = 0; if ((x>0) && (y>0)) { z = x; } return z; } input x 1 0 y 1 0 expected 1 0 int z = 0; if ((x>0) && (y>0)) { true (a) z = x; (b) return z; Paths: ✔ • a , b ✔ • c (c) false
Now try this code… Control Flow Graph int result=0; result = x*2; x < 69 T F x = 6969; x % 2 == 0 T F x > 6969 F public int bar(int x) { int result=0; if (x < 69) { if (x % 2 == 0) { result = x/2; } else { result = x*2; } } else if (x > 6969) { x = 69; } else { x = 6969; } return result; } result = x/2; T x = 69; return result;
Now try this code… Control Flow Graph Input (x) expected 1 2 6970 70 2 1 69 6969 int result=0; result = x*2; (a) x < 69 (b) F x > 6969 (c) F x = 6969; T (d) (f) F x % 2 == 0 (h) (g) T result = x/2; T x = 69; (e) (k) (i) (j) Paths: ✔ • a , d , f , h ✔ • a , d , g , i ✔ • a , b , e , j ✔ • a , b , c , k return result;
BONUS QUESTION
Given this code 1. Draw a control flow graph 2. Define test suites that provide: • Statement coverage • Condition coverage • Path coverage int my. F(int x, int y) { while (x > 10) { x = x – 10; if (x == 10) { break; } } if (y < 20 && x%2 == 0) { y = y + 20; } else { y = y – 20; } return 2*x + y; }
int my. F(int x, int y) { while (x > 10) { x = x – 10; if (x == 10) { break; } } if (y < 20 && x%2 == 0) { y = y + 20; } else { y = y – 20; } return 2*x + y; } while (x > 10) T F x = x – 10; if (x == 10) T break; if (y < 20 && x%2 == 0) F y = y – 20; Here’s the CFG F T y = y + 20; return 2*x + y;
- Slides: 20