Title: White Box Testing
1White Box Testing
- Dynamic Analysis Technique White Box Testing
- Why and How?
2Agenda
- Announcements
- Reminder
- Lab 7 due Thursday night _at_ 11 PM
- Lecture
- White Box Testing
3White Box Testing
- Uses structure of program to derive test cases
- Set of test cases case guarantee
- All independent paths have been executed at least
once - Exercise all logical decisions, both T and F
- Execute all loops at their boundaries and within
bounds - Exercise internal data structures to ensure
validity
4Why White Box Testing?
- Why not concentrate on program requirements?
- Nature of defects
- Logic errors are inversely proportional to the
probability that a program path will be executed - Believe that a logical path is not likely to be
executed, when it is actually executed on a
regular basis - Typographical errors are random
5Why White Box Testing?
- Why not focus on program requirements?
- Nature of Defects
- Logic errors are inversely proportional to the
probability that a program path will be executed - Believe that a logical path is not likely to be
executed, when it is actually executed on a
regular basis - Typographical errors are random
6White Box Testing Techniques
- Statement Coverage
- Make sure each statement in code is executed at
least once - Branch Decision Coverage
- Make sure each path in code is executed at least
once - Data Flow Testing
- Uses data to select path of program to execute
7Statement Coverage How To
- Using code, draw a corresponding flow graph
- Determine the cyclomatic complexity of the flow
graph - Determine a set of independent paths
- Prepare test cases that will force execution of
each path
8Flow Graph Techniques
- Loops
- While
- Until/Do While
F
Statement 1 Statement 2 Statement 3
while X lt 10 Statement A x x
1 Statement B
1
2
3
T
while
A
x
B
if
F
T
if X gt 0 Statement A else Statement
B Statement C
do Statement A x x 1 until/while
X lt 10 Statement B
A
B
F
A
x
while
B
C
T
9Program Flow Graphs
- Way of visualizing complexity
- Number or paths through program
1
Simple Example
Statement 1 Statement 2 if X lt 10 then
Statement 3 Statement 4
2
T
3
if
F
4
10How to Measure Complexity
- McCabes complexity
- Metric calculated by counting the number of
decision points in a routine - Cyclomatic Complexity
- V(G) E - N 2
- Determining Number of Decision Points
- 1. Start with 1 for the straight path through
the routine - 2. Add 1 for each of the following keywords, or
their equivalents if, while, repeat, for, and,
or - 3. Add 1 for each case in a case statement.
11Code Fragment - Bubble Sort
Public static void bubbleSort2 (Sequence S)
Position prec, succ int n S.size() for
(int i 0 i lt n i) // i-th pass prec
S.first() for (int j1 j lt n - i j)
succ S.after(prec) if (valAtPos(prec)
gt valAtPos(succ)) S.swapElements(prec,
succ) prec succ
12Bubble Sort Example
declarations
int size
for
T (i lt n)
prec
F (j gt n)
F (i gt n)
for
T (j lt n)
T
succ
if precgtsucc
S.swap
precsucc
F
End Routine
13Example 2 (PDL)
PROCEDURE average This program computes the
average of 100 or fewer numbers that lie between
bounding values it also computes the sum and the
total number valid TYPE value1100 is SCALAR
ARRAY TYPE average, total.input,
total.valid minimum, maximum, sum IS
SCALAR TYPE I is INTEGER I 1 total.input
total.valid 0 sum 0 DO WHILE valueI ltgt
-999 AND total.input lt 100 increment total.input
by 1 IF valueI gt minimum AND valueI lt
maximum THEN increment total.valid by 1 sum
sum valueI ENDIF increment I by
1 ENDWHILE IF total.valid gt 0 THEN average
sum/total.valid ELSE average -999 ENDIF END
14Flow Graph
A
F
T
while Value ! -999
if valid gt 0
Compute average
End
T
F
F
avg -999
while Input lt 100
T
inc
T
T
if Value gt min
if Value lt max
B
F
F
i
15Example 2 - Test Cases
- Nominal Case
- All conditions are true
- Input 5th element of array is between min max
- While is false
- Because 1st condition is false
- Input 1st element of array is -999
- Because 2nd condition is false
- Input 101st element of array is ! -999
- If 1 is false
- Because 1st condition is false
- Input Nth element of array Value N lt minimum
- Because 2nd condition is false
- Input Nth element of array ValueN gt maximum
- If 2 is false
- Because there are no valid elements in array
- Input Array of N elements, where Value1..N-1
are all below min or above max, and Nth element
is -999
16Example 3 (Pascal)
Compute Net Pay TtlWithholdings 0 for ID
1 to NumEmployees begin compute
Social Security withholding, if below the
maximum) if (EmployeeID.SSWithheld lt
MAX_SOCIAL_SECURITY) then SocialSecurity
ComputeSocialSecurity(EmployeeID) set
default to no retirement contribution
Retirement 0 determine discretionary
employee retirement contribution if
(EmployeeID.WantsRetirement) and
(EligibleForRetirement(EmployeeID))
then Retirement GetRetirement
(EmployeeID) GrossPay ComputeGrossPay
(EmployeeID) determine IRA
contribution IRA 0 if
(EligibleForIRA(EmployeeID)) then IRA
IRAContribution (EmployeeID, Retirement,
GrossPay) make weekly paycheck
Withholding ComputeWithholding
(EmployeeID) NetPay GrossPay -
Withholding - Retirement - SocialSecurity
- IRA PayEmployee (EmployeeID, NetPay)
end for
17Flow Graph
Ttl...
for
T
if 1
Soc...
F
Ret...
T
if 2a
if 2b
T
F
F
F
Gro...
Ret...
With NetP PayE...
T
F
if 3
end
IRA...
18Example 3 Test Cases
- Nominal Case
- All boolean conditions are true
- Initial for condition is false
- NumEmployees lt 1
- 1st if is false
- Withholding amount is gt MAX
- 2nd if is false because first part of and is
false - Employee doesnt want retirement
- 2nd if is false because second part of and is
false - Employee isnt eligible for retirement
- 3rd if is false
- Employee not eligible for IRA