White Box Testing
-
Upload
alessandro100 -
Category
Documents
-
view
3.938 -
download
0
Transcript of White Box Testing
White‐boxtes,ngCorsodiLaureaMagistraleinIngegneriaInforma,ca
Lecturer:Valen,naPresu>AcademicYear:2008/2009
Summary
• White‐box(orGlass‐box)tes,ng:generalcharacteris,cs
• Statementcoverage
• Decisioncoverage• Condi,oncoverage• Decisionandcondi,oncoverage• Coverageofalinearlyindependentsetofpaths
SoNwareEngineeringa.a.2008/2009White‐boxtes,ng 2
White‐boxtes,ng:generalcharacteris,cs
• Alsocalled– Glass‐boxtes,ng– Structuraltes,ng(comparedtofunc,onal)
• Exploitsthecontrolstructure,thatmeansthecontrolorflowgraphofprogramsinordertodesigntestcases
• Typicallyperformedduringcoding
SoNwareEngineeringa.a.2008/2009White‐boxtes,ng 3
White‐boxtes,ng:generalcharacteris,cscont.d
• Allowstotestpartsoftheprogram– Withblack‐boxisnotpossible
• Allowstocoversystema,callyeverypartoftheprogram– ONenerrorsarefoundinspecialcases
SoNwareEngineeringa.a.2008/2009White‐boxtes,ng 4
Typesofwhite‐boxtests
SoNwareEngineeringa.a.2008/2009White‐boxtes,ng 5
Statementcoverage
• Itistheeasiestcriterionforareasonablewhite‐boxtest
• Necessarybutnotsufficient
• Goal– Toiden,fyasetoftestcasessufficienttoexerciseallstatementsatleastonce
SoNwareEngineeringa.a.2008/2009White‐boxtes,ng 6
Statementcoveragecont.d
• Thetestcase{x!=0;y=any}
• Coversthestatements
SoNwareEngineeringa.a.2008/2009White‐boxtes,ng 7
Javaexample:
/*A*/floatx=InOut.readFloat(),y=InOut.readFloat();/*B*/if(x!=0)/*C*/y+=10;/*D*/y=y/x;/*E*/System.out.println(x+''+y);
A
B
C
D
E
x==0
x!=0
Controlflowgraphfortheexample
Statementcoveragecont.d
• Possibleproblem– Eveninacaseofaprogramwithoutitera,ons,theexecu,onofeverystatementdoesnotguaranteethatallpossiblepathsareexercised
• Possiblenega,veconsequence:– Inpreviousjavaexampleprogramtheerrorofdivisionby0isnotiden,fied(statementD)
SoNwareEngineeringa.a.2008/2009White‐boxtes,ng 8
Decisioncoverage
• Properlyincludesstatementcoverage• Viceversadoesnothold• Focusonallpossibledecisionsinaprogram• Decisionsareinstatements
– if,switch,while,for,do• Goal
– Toiden,fyasetoftestcasessufficientforguaranteeingthateachdecisionwillhavevalue“true”atleastonceandvalue“false”atleastonce
SoNwareEngineeringa.a.2008/2009White‐boxtes,ng 9
Decisioncoveragecont.d
SoNwareEngineeringa.a.2008/2009White‐boxtes,ng 10
Javaexample:
/*A*/floatx=InOut.readFloat(),y=InOut.readFloat();/*B*/if(x!=0)/*C*/y+=10;/*D*/y=y/x;/*E*/System.out.println(x+''+y);
A
B
C
D
E
x==0
x!=0
Testcases1. {x=20;y=30}2. {x=0;y=30}
Coverdecisionshencestatements.Thesecondcaseiden,fiestheerrorofdivisionbyzeroinstatementD
Decisioncoveragecont.d
• Possibleproblem• Ifdecisionsarecomposedofseveralcondi,ons(AND,OR),decisioncoveragecanbenotsufficient
SoNwareEngineeringa.a.2008/2009White‐boxtes,ng 11
EsempioJava:/*A*/floatz=InOut.readFloat(),w=InOut.readFloat();t=InOut.readFloat();/*B*/if(z==0||w>0)/*C*/w=w/z;/*D*/elsew=w+2/t;/*E*/System.out.println(z+''+w+''+t);
Whatistheproblemhere?
Decisioncoveragecont.d
• Testcases:– {t=0;z=5;w=5}decisionTRUE– {t=0;z=5;w=‐5}decisionFALSE
• Coverdecision• Thesecondoneiden,fiesthedivisionbyzeroinDbut
• TheriskofdivisionbyzeroinCisnotiden,fies• Weneedacriterionthatconsidersbothstructureandcomponentsofdecisions
SoNwareEngineeringa.a.2008/2009White‐boxtes,ng 12
Condi,oncoverage
• Doesnotproperlyincludedecisioncoverage• Itisnotproperlyincludedbydecisioncoverage• Focusonallpossiblecondi,onsinaprogram• Condi,onsaretheoperandsofnon‐atomicbooleanexpressions
• Goal:– Toiden,fyasetoftestcasessufficientforguaranteeingthateverycondi,on(atomicbooleanexpression)includedintheprogram’sdecisionshavevalue“true”atleastonceandvalue“false”atleastonce
SoNwareEngineeringa.a.2008/2009White‐boxtes,ng 13
Condi,oncoveragecont.d
SoNwareEngineeringa.a.2008/2009White‐boxtes,ng 14
Testcases{t=0;z=0;w=‐5}1ac.=T,2ac.=F{t=0;z=5;w=5}1ac.=F,2ac.=TCovercondi,ons
Whatproblemdosuchcasetestsmakeemerge?
Javaexample:/*A*/floatz=InOut.readFloat(),w=InOut.readFloat();t=InOut.readFloat();/*B*/if(z==0||w>0)/*C*/w=w/z;/*D*/elsew=w+2/t;/*E*/System.out.println(z+''+w+''+t);
Condi,oncoverage
• Testcases– {t=0;z=0;w=‐5}1acondi,on=T,2acondi,on=F– {t=0;z=5;w=5}1acondi,on=F,2acondi,on=T
• Covercondi,ons• Thefirstoneiden,fiesthedivisionbyzeroin
C• TheriskofdivisionbyzeroinDisnot
iden,fiedasDisneverexcercised• Decisionisalways“true”
SoNwareEngineeringa.a.2008/2009White‐boxtes,ng 15
Decisionandcondi,oncoverage
• Properlyincludes– Decisioncoverage– Condi,oncoverage
• Goal– Toiden,fyasetoftestcasessufficientforguaranteeingthat• Eachdecisionis“true”atleastonceand“false”atleastonce
• Allcondi,onscomposingdecisionsis“true”atleastonceand“false”atleastonce
SoNwareEngineeringa.a.2008/2009White‐boxtes,ng 16
Decisionandcondi,oncoveragecont.d
SoNwareEngineeringa.a.2008/2009White‐boxtes,ng 17
Testcases{t=0;z=0;w=5}1ac.=T,2ac.=T,dec.=T{t=0;z=5;w=‐5}1ac.=F,2ac.=F,dec.=FCoverdecisionsandcondi,onsandiden,fybothdivisionbyzero
instatementsCandD
/*A*/floatz=InOut.readFloat(),w=InOut.readFloat();t=InOut.readFloat();/*B*/if(z==0||w>0)/*C*/w=w/z;/*D*/elsew=w+2/t;/*E*/System.out.println(z+''+w+''+t);
Summaryoftestcasessofar
SoNwareEngineeringa.a.2008/2009White‐boxtes,ng 18
Testcase
t z w cond.1 cond.2 decis. FindserrorinC?
FindserrorinD?
C1 0 0 5 T T T yes no
C2 0 5 5 F T T no no
C3 0 0 ‐5 T F T yes no
C4 0 5 ‐5 F F F no yes
SoNwareEngineeringa.a.2008/2009White‐boxtes,ng 19
Coveragecriterion
Associatedtestcases
FindserrorinC?
FindserrorinD?
Decision C2+C4 no yes
Condi,on C2+C3 yes no
Decisionandcondi,on
C1+C4,or
C2+C3+C4 yes yes
Methodologicalissue
• Therearedifferentcombina,onoftestcasesthatguaranteedecisionandcondi,oncoveragebutitisnotalwaystrivialtofindone
• Weneedasimpleandeffec,vemethodforiden,fyingtestcasesthatcoverbothdecisionsandcondi,ons
SoNwareEngineeringa.a.2008/2009White‐boxtes,ng 20
Controlflowgraph’spathscoverage
• Basedon– Criteriaofdecisionandcondi,oncoverage– Program’scontrolflowgraph
• Goal– Toprovideasimplemethodforguaranteeingdecisionandcondi,oncoverage
SoNwareEngineeringa.a.2008/2009White‐boxtes,ng 21
Controlflowgraph
SoNwareEngineeringa.a.2008/2009White‐boxtes,ng 22
while(A){if(B){C}else{D}E}F;
node
arc
region Example:V(G)=7‐6+2=3V(G)=3
Predicatenodes
A
B
CD
E
F
Cycloma,cnumber• V(G)=E‐N+2(E=numberofarcs,N=numberofnodes)• V(G)=R(R=numberofregions)
Controlflowgraph
SoNwareEngineeringa.a.2008/2009White‐boxtes,ng 23
Sequenceofstatements
Simpleif
un,l
whilecase
Controlflowgraphwithoutcondi,ons
SoNwareEngineeringa.a.2008/2009White‐boxtes,ng 24
A
B
CD
E
False True
Thecontrolflowgraphshouldbemoredetailed
/*A*/floatz=InOut.readFloat(),w=InOut.readFloat();t=InOut.readFloat();/*B*/if(z==0||w>0)/*C*/w=w/z;/*D*/elsew=w+2/t;/*E*/System.out.println(z+''+w+''+t);
Controlflowgraphwithcomplexdecisions
SoNwareEngineeringa.a.2008/2009White‐boxtes,ng 25
if(a||b)x;elsey;
b==F
...
a
b
xy
...
a==Ta==F
b==T
if(a&&b)
x;elsey;
...
a
b
yx
...
a==Fa==T
b==Fb==T
Controlflowgraphfortheprogram
SoNwareEngineeringa.a.2008/2009White‐boxtes,ng 26
A
B
CD
E
False True
A
z
w
CD
E
==0!=0
<=0 >0
ControlflowgraphWithoutcondi:ons
Controlflowgraphwithcondi:ons
/*A*/floatz=InOut.readFloat(),w=InOut.readFloat();t=InOut.readFloat();/*B*/if(z==0||w>0)/*C*/w=w/z;/*D*/elsew=w+2/t;/*E*/System.out.println(z+''+w+''+t);
Controlflowgraph,condi,onsanddecisions
• Booleanvalues(“true”and“false”)associatedwithcondi,onsanddecisionshavecorrepondingarcs
• Todesignasetoftestcasessuchthatallarcsofthecontrolflowgrapharetraversedimpliescondi,onanddecisioncoverage
SoNwareEngineeringa.a.2008/2009White‐boxtes,ng 27
Pathsofthecontrolflowgraph
• Thesetoftestcasescorrespondstoasetofpathssuchthatalleveryarcistraversedatleastonce
• Thenumberofpathssufficientforcoveringallarcsisalwaysequalorlessthanthecycloma,ccomplexity
SoNwareEngineeringa.a.2008/2009White‐boxtes,ng 28
Forthepreviousexample
SoNwareEngineeringa.a.2008/2009White‐boxtes,ng 29
ThreetestcasesC2.{t=0;z=5;w=5}C3.{t=0;z=0;w=‐5}C4.{t=0;z=5;w=‐5}Coverallarcs,hencedecisionsandcondi,ons
V=7‐6+2=3Thethreepaths1. A‐z‐w‐C‐E (C2)2. A‐z‐C‐E (C3)3. A‐z‐w‐D‐E (C4)
/*A*/floatz=InOut.readFloat(),w=InOut.readFloat();t=InOut.readFloat();/*B*/if(z==0||w>0)/*C*/w=w/z;/*D*/elsew=w+2/t;/*E*/System.out.println(z+''+w+''+t);
A
z
w
CD
E
==0!=0
<=0 >0
Howtochoosepaths
• Pragma,crule– Experimentsshowthatthethenumberoferrors
increasewithincreasingofthecycloma,ccomplexity
– Chooseanumberofpathsequaltothecycloma,ccomplexity
– Youhaveapropor,onbetweennumberoftestcasesandcomplexityofthecode
SoNwareEngineeringa.a.2008/2009White‐boxtes,ng 30
Linearlyindependentpaths
• Agoodcriterionforchoosingpathsisbasedontheconceptoflinearindependency
• Amaximalsetoflinearlyindependentpathsiscalledabase– ItisNOTunique
• Abasecontainsanumberofpathsequaltothecycloma,ccomplexity
• Intui,vely,everypathinput‐outputcanbeobtainedasalinearcombina,onofpathsofabase
• Bychoosingabase,acertainextendofreliabilityidguaranteedwithrespectofcombina,onsoferrorsthathideeachother
SoNwareEngineeringa.a.2008/2009White‐boxtes,ng 31
Exampleofpaths
SoNwareEngineeringa.a.2008/2009White‐boxtes,ng 32
while(A){if(B){C}else{D}E}F;
Pathsα. A‐Fβ. A‐B‐D‐E‐A‐Fγ. A‐B‐C‐E‐A‐F
A
B
CD
E
F
1
2 3
4 5
67
Arcs/paths
1 2 3 4 5 6 7
α 0 0 0 0 0 0 1 β 1 1 0 1 0 1 1 γ 1 0 1 0 1 1 1
Thematrixrankis3(themaximum){αβγ}isabase
Remarks• Everypathcorrespondstoabinaryrowvector(0/1)• Viceversadoesnothold• Amatrixrepresen,ngpathshasmaximum2Edis,nct
rows(E=n°archi)• McCabeprovedthatforeachgraphG:
1. ThematrixrankcannotbegreaterthanV(G)(cycloma,ccomplexity)
2. ThereexistsalwaysamatrixmadeofpathswithrankequaltoV(G)
• Abaseisanysetofpaths(rows)withmaximumrank• Note:MacCabe’sresultispurelytopological
/*B*/if(5>7)/*C*/i=9• ArcB‐Cisnevercovered• Inothercasesisnecessarytore‐iteratethecyclesin
ordertotraverseallarcs
SoNwareEngineeringa.a.2008/2009White‐boxtes,ng 33
A
B
CD
E
F
1
2 3
4 5
67
Arcstraversableonlybyitera,ons
SoNwareEngineeringa.a.2008/2009White‐boxtes,ng 34
/*A*/intz=InOut.readInt();intt=0;/*B*/while(t<100)/*C*/ if(t>20)/*D*/t=t+90;/*E*/elset=t+z*z;/*F*/System.out.println(t);
A
B
C
ED
K
t>=100t<100
t<=20t>20
F
B‐FC‐DRequireatleastoneitera,oninordertobetraversed{10}A‐B‐C‐E‐K‐B‐F{5}A‐B‐C‐E‐K‐B‐C‐D‐K‐B‐F
Fakenode
Addi,onalconsidera,ons
• Pathscoveragemethoddoesnotconsiderexplicitlyloopsitera,ons
• Insomecasesitisnecessarytoperformitera,onsinordertotraverseallarcs
• Itmightbeinteres,ngtoconsidersomeitera,oninanycasebutthenumberoftestcasesincreasesexponen,ally
• Itisimportanttodecidethetypeofitera,ons
SoNwareEngineeringa.a.2008/2009White‐boxtes,ng 35
Pragma,cchoicesforloops
• Tolimitthenumberofitera,onston– Itiscalled“loopcoverage”
• Toexecuteonlycertainloops• Tolimitthenumberofpathstobetraversedbasedonweightedarcsandfunc,ontobemaximized– Probabilityofexecu,on– Resourcesoccupancy
• Tolimitthenumberofpathsbyiden,fyingthepathsthatdefineanduseprogramvariables(DataFlowtes,ng)– Defini,onofavariablevalue– Useofsuchvalueinatest
SoNwareEngineeringa.a.2008/2009White‐boxtes,ng 36
DataFlowTes,ng
SoNwareEngineeringa.a.2008/2009White‐boxtes,ng 37
...Aintx,y,a,b;Bscanf(“%d%d”,&x,&y);Ca=x;Db=yEwhile(a!=b)Fif(a>b)Ga=a‐b;Helseb=b‐a;Iprin~(“%d”,a);....
D
E
I
F
a!=b
C
B
HG
a==b
a<=ba>b
Forxandyitisnotneededtoexecutetheloop(loopcoverage=0)Forthedefini,onofaandbtheloophastobeiteratedonce(loopcoverage=1)Forthedefini,onandusageofaandbtheloophastobeiteratedtwice(loopcoverage=2)