Presentation is loading. Please wait.

Presentation is loading. Please wait.

White-Box Testing Techniques. Definition of White-Box Testing Testing based on analysis of internal logic (design, code, etc.). (But expected results.

Similar presentations


Presentation on theme: "White-Box Testing Techniques. Definition of White-Box Testing Testing based on analysis of internal logic (design, code, etc.). (But expected results."— Presentation transcript:

1 White-Box Testing Techniques

2 Definition of White-Box Testing Testing based on analysis of internal logic (design, code, etc.). (But expected results still come from requirements.) Also know as structural testing. White-box testing concerns techniques for designing tests; it is not a level of testing. White-box testing techniques apply primarily to lower levels of testing (e.g., unit and component).

3 3 Motivation People are not perfect –We make errors in design and code Goal of testing: given some code, uncover as many errors as possible Important and expensive activity: –May spend 30-40% of total project effort on testing –For safety critical system cost of testing is several times higher than all other activities combined

4 4 A Way of Thinking Design and coding are creative activities Testing is destructive –The primary goal is to “break” the code Often same person does both coding and testing –Need “split personality”: when you start testing, become paranoid and malicious –This is surprisingly difficult: people don’t like to find out that they made mistakes.

5 5 Testing Objective Testing: a process of executing software with the intent of finding errors Good testing: a high probability of finding as-yet- undiscovered errors Successful testing: discovers unknown errors

6 6 Basic Definitions Test case: specifies –Inputs + pre-test state of the software –Expected results (outputs an state) Black-box testing: ignores the internal logic of the software, and looks at what happens at the interface (e.g., given this inputs, was the produced output correct?) White-box testing: uses knowledge of the internal structure of the software –E.g., write tests to “cover” internal paths

7 7 Control-flow-based Testing A traditional form of white-box testing Step 1: From the source, create a graph describing the flow of control –Called the control flow graph –The graph is created (extracted from the source code) manually or automatically Step 2: Design test cases to cover certain elements of this graph –Nodes, edges, paths (types of logic coverage)

8 Types of Logic Coverage Statement: each statement executed at least once Branch: each branch traversed (and every entry point taken) at least once Condition: each condition True at least once and False at least once Branch/Condition: both Branch and Condition coverage achieved

9 Types of Logic Coverage (cont’d) Compound Condition: all combinations of condition values at every branch statement covered (and every entry point taken) Path: all program paths traversed at least once

10 10 Example of a Control Flow Graph (CFG) if (x+y < 100) s:=s+x+y; else d:=d+x-y; } 1 2 3 4 5 6 7 8 s:=0; d:=0; while (x<y) { x:=x+3; y:=y+2;

11 CFG representations for Program Constructs

12 Example 1 if a then s1 else if b then s2 else if c then s3 else s4 end_if_then_else 12

13 Example 2 if a then s1 end_if_then if b then s2 end_if_then if c then s3 end_if_then 13

14 Example 3 while a do if b then s1 else s2 end_if_then_ else end_while 14

15 15 int f1(int x,int y){ 1 while (x != y){ 2 if (x>y) then 3 x=x-y; 4 else y=y-x; 5 } 6 return x; } 1 2 34 5 6 Example 4

16 Pseudocode and Control Flow Graphs input(Y) if (Y<=0) then Y := −Y end_if while (Y>0) do input(X) Y := Y-1 end_while “nodes” “edges”

17 What if you could test all paths? Would the program be correct then? –The program might be missing paths Code for branch alternative not there More likely detect missing paths by requirements testing –Might have computational errors not revealed in the test data shown, even on the path where the error occurs We call that coincidental correctness For example, suppose code has x = A + A where it should have A*A but you test with A = 2. 17

18 When are you done with unit testing? Decide on criterion –Statement coverage –Branch coverage –Logical path coverage 18 1 2 3 4 5 Statement: 1, 2, 3, 4, 5 Branch: 1, 2, 3, 4, 5 1, 3, 5 Path: 1, 2, 3, 4, 5 1, 3, 5 1, 2, 3, 5 1, 3, 4, 5

19 Statement Coverage Statement Coverage requires that each statement will have been executed at least once. Simplest form of logic coverage. Also known as Node Coverage. What is the minimum number of test cases required to achieve statement coverage for the program segment given below?

20 How many test cases required for Statement Coverage? input(Y) if (Y<=0) then Y := −Y end_if while (Y>0) do input(X) Y := Y-1 end_while

21 Branch Coverage Branch Coverage requires that each branch will have been traversed, and that every program entry point will have been taken, at least once. Also known as Edge Coverage.

22 Branch Coverage (cont’d) Why “…and that every program entry point will have been taken, at least once.”

23 Branch Coverage (cont’d) Why “…and that every program entry point will have been taken, at least once.”

24 Branch Coverage (cont’d) What is the relationship between Statement and Branch Coverage?

25 Branch Coverage (cont’d) What is the relationship between Statement and Branch Coverage? Possible relationships: 1.None. 2.Statement Coverage subsumes Branch Coverage (“statement => branch”). 3.Branch Coverage subsumes Statement Coverage (“branch => statement”).

26 Does “statement => branch” ??? Min. number of cases required for Statement Coverage? Min. number of cases required for Branch Coverage?

27 Does “branch => statement” ??? Normally, YES…

28 Does “branch => statement” ??? Normally, YES… in the absence of “DEAD CODE”. DEAD CODE is not reachable via any executable program path.

29 Does “branch => statement” ??? If a program has "dead (i.e., unreachable) code", then "statement coverage" is unachievable. (We would need to modify the program in order to bring the dead code back to “life”.) Bottom line: we will always assume the nominal case of “no dead code" unless explicitly stated otherwise. Under this assumption, Branch Coverage does indeed subsume Statement Coverage.

30 Condition Coverage A branch predicate may have more than one condition. input(X,Y) if (Y<=0) or (X=0) then Y := -Y end_if while (Y>0) and (not EOF) do input(X) Y := Y-1 end_while

31 Condition Coverage (cont’d) Condition Coverage requires that each condition will have been True at least once and False at least once. What is the relationship between Branch and Condition Coverage?

32 Condition Coverage (cont’d) if A or B then s1 else s2 end_if_then_else ABBranch test 1TFtrue test 2FFfalse

33 Condition Coverage (cont’d) if A or B then s1 else s2 end_if_then_else ABBranch test 3TFtrue test 4FTtrue

34 Branch/Condition Coverage Branch/Condition Coverage requires that both Branch AND Condition Coverage will have been achieved. Therefore, Branch/Condition Coverage subsumes both Branch Coverage and Condition Coverage.

35 Compound Condition Coverage What if the compiler generates code that masks the evaluation of conditions? That is, suppose if (A) or (y/x=5) then... is compiled in such a way that if A is true, y/x=5 will not be evaluated.

36 Compound Condition Coverage (cont’d) Compound Condition Coverage requires that all combinations of condition values at every branch statement will have been covered, and that every entry point will have been taken, at least once. Also know as Multiple Condition Coverage. Subsumes Branch/Condition Coverage, regardless of the order in which conditions are evaluated.

37 Compound Condition Coverage (cont’d) input(X,Y) if (Y<=0) or (X=0) then Y := -Y end_if Combinations of condition values: TT, TF, FT, FF

38 Compound Condition Coverage (cont’d) In general, how many different combinations of condition values must be considered when a branch predicate has N conditions?

39 Path Coverage Path Coverage requires that all program paths will have been traversed at least once. Often described as the “strongest” form of logic coverage? (Is it stronger than Compound Condition Coverage?) Path Coverage is usually impossible when loops are present. (How many test cases would be required to cover all paths in the example below?)

40 Path Coverage (cont’d) for I = 1 to 30 do input(X,Y) if (Y<=0) then if (X<=0) then Y := -X else Y:=-Y end_if_else else Y := X+Y end_if_else end_for_do repeat 29 times

41 Path Coverage (cont’d) 3 paths 3 X 3 = 9 paths

42 Path Coverage (cont’d) repeat 29 times 3 X 3 X…X 3 = 3 paths 30

43 Path Coverage (cont’d) Various strategies have been developed for identifying useful subsets of paths for testing when Path Coverage is impractical: –Loop Coverage, –Basis Paths Coverage, and –Dataflow Coverage.

44 Loop Coverage Loop Coverage requires that the body of loops be executed 0, 1, 2, t, max, and max+1 times, where possible. Rationale: –0: Is some action taken in the body that must also be taken when the body is not executed? –1: Check lower bound on number of times body may be executed.

45 Loop Coverage (cont’d) Rationale: (cont’d) –2: Check loop re-initialization. –t: Check typical number of iterations. –max: Check upper (valid) bound on number of times body may be executed. –max+1: If the maximum can be exceeded, what behavior results?

46 Basis Paths Coverage A coverage criterion associated with McCabe’s Structured Testing. Based on idea of identifying a “spanning” (i.e., basis) set of paths for a program’s “path space.” The number, C, of such paths is equal to –number of (2-way) branch statements in the program + 1. –This is also the number of enclosed regions in the program graph + 1 –Number edges-Number of nodes+2

47 Basis Paths Coverage (cont’d) C is what McCabe calls the Cyclomatic Complexity of a program. Any C distinct, simple program paths that provide branch coverage also form a basis set of paths. (In a simple program path, loop bodies are executed at most once.)

48 48 An interesting application of cyclomatic complexity Cyclomatic complexity indicates: –the number of errors existing in the code, –the time required to find and correct the errors. –the psychological complexity of a program. –difficulty level of understanding the program. From maintenance perspective, –limit cyclomatic complexity of modules to some reasonable value. –Good software development organizations restrict cyclomatic complexity of functions to a maximum of ten or so.

49 49 Cyclomatic complexity From maintenance perspective, –limit cyclomatic complexity of modules to some reasonable value. –Good software development organizations: restrict cyclomatic complexity of functions to a maximum of ten or so.

50 50 Example int f1(int x,int y){ 1 while (x != y){ 2 if (x>y) then 3 x=x-y; 4 else y=y-x; 5 } 6 return x; } 1 2 34 5 6 Cyclomatic Complexity=2+1=3 or 6-6+2=2 Number of independent paths: 3 –1,6 test case (x=1, y=1) –1,2,3,5,1,6 test case(x=1, y=2) –1,2,4,5,1,6 test case(x=2, y=1)

51 Exercise 1 if a then s1 else if b then s2 else if c then s3 else s4 end_if_then_else Calculate cylomatic complexity and design test cases using basis path coverage criterion.

52 Exercise 2 if a then s1 end_if_then if b then s2 end_if_then if c then s3 end_if_then Calculate cylomatic complexity and design test cases using basis path coverage criterion.

53 Exercise 3 while a do if b then s1 else s2 end_if_then_else end_while Calculate cylomatic complexity and design test cases using basis path coverage criterion.

54 In General… Number of program Paths Number of Basis Paths Number of test cases required for branch coverage  Path Coverage Basis Paths Coverage Branch Coverage =>

55 Summary of White-Box Coverage Relationships (so far…) Path Compound Condition Branch / Condition Basis PathsLoop Condition Statement Branch

56 More Challenges Interactive programs Listeners or event-driven programs Concurrent programs Exceptions Self-modifying programs Mobile code Constructors/destructors Garbage collection

57 Specification-Based Testing Use specifications to derive test cases –Requirements –Design –Function signature Based on some kind of input domain Choose test cases that guarantee a wide range of coverage –Typical values –Boundary values –Special cases –Invalid input values

58 “Some Kind of Input Domain” Determine a basis for dividing the input domain into subdomains –Subdomains may overlap Possible bases –Size –Order –Structure –Correctness –Your creative thinking Select test cases from each subdomain –One test case may suffice

59 Example 1float homeworkAverage(float[] scores) { 2 float min = 99999; 3 float total = 0; 4 for (int i = 0 ; i < scores.length ; i++) { 5 if (scores[i] < min) 6 min = scores[i]; 7 total += scores[i]; 8 } 9 total = total – min; 10 return total / (scores.length – 1); 11}

60 Possible Bases Array length –Empty array –One element –Two or three elements –Lots of elements Input domain: float[] Basis: array length one small empty large

61 Possible Bases Position of minimum score –Smallest element first –Smallest element in middle –Smallest element last Input domain: float[] Basis: position of minima somewhere in middle first last

62 Possible Bases Number of minima –Unique minimum –A few minima –All minima Input domain: float[] Basis: number of minima all data equal 1 minimum 2 minima

63 Testing Matrix Test case (input) Basis (subdomain) Expected output Notes

64 Challenges Structural testing can cover all nodes or edges without revealing obvious faults –No matter what input, program always returns 0 Some nodes, edges, or loop combinations may be infeasible –Unreachable/unexecutable code “Thoroughness” –A test suite that guarantees edge coverage also guarantees node coverage… –…but it may not find as many faults as a different test suite that only guarantees node coverage

65 homeworkAverage 1 Test case (input) Basis: Array lengthExpected output Notes EmptyOneSmallLarge ()x0.099999! (87.3)x87.3 crashes! (90,95,85)x92.5 (80,81,82,83, 84,85,86,87, 88,89,90,91) x86.0

66 homeworkAverage 2 Test case (input) Position of minimumExpected output Notes FirstMiddleLast (80,87,88,89)x88.0 (87,88,80,89)x88.0 (99,98,0,97,96)x97.5 (87,88,89,80)x88.0

67 homeworkAverage 3 Test case (input) Number of minimaExpected output Notes OneSeveralAll (80,87,88,89)x88.0 (87,86,86,88)x87.0 (99,98,0,97,0)x73.5 (88,88,88,88)x88.0


Download ppt "White-Box Testing Techniques. Definition of White-Box Testing Testing based on analysis of internal logic (design, code, etc.). (But expected results."

Similar presentations


Ads by Google