white box testing

37
White‐box tes,ng Corso di Laurea Magistrale in Ingegneria Informa,ca Lecturer: Valen,na Presu> Academic Year: 2008/2009

Upload: alessandro100

Post on 26-May-2015

3.938 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: White Box Testing

White‐boxtes,ngCorsodiLaureaMagistraleinIngegneriaInforma,ca

Lecturer:Valen,naPresu>AcademicYear:2008/2009

Page 2: White Box Testing

Summary

•  White‐box(orGlass‐box)tes,ng:generalcharacteris,cs

•  Statementcoverage

•  Decisioncoverage•  Condi,oncoverage•  Decisionandcondi,oncoverage•  Coverageofalinearlyindependentsetofpaths

SoNwareEngineeringa.a.2008/2009White‐boxtes,ng 2

Page 3: White Box Testing

White‐boxtes,ng:generalcharacteris,cs

•  Alsocalled– Glass‐boxtes,ng– Structuraltes,ng(comparedtofunc,onal)

•  Exploitsthecontrolstructure,thatmeansthecontrolorflowgraphofprogramsinordertodesigntestcases

•  Typicallyperformedduringcoding

SoNwareEngineeringa.a.2008/2009White‐boxtes,ng 3

Page 4: White Box Testing

White‐boxtes,ng:generalcharacteris,cscont.d

•  Allowstotestpartsoftheprogram– Withblack‐boxisnotpossible

•  Allowstocoversystema,callyeverypartoftheprogram– ONenerrorsarefoundinspecialcases

SoNwareEngineeringa.a.2008/2009White‐boxtes,ng 4

Page 5: White Box Testing

Typesofwhite‐boxtests

SoNwareEngineeringa.a.2008/2009White‐boxtes,ng 5

Page 6: White Box Testing

Statementcoverage

•  Itistheeasiestcriterionforareasonablewhite‐boxtest

•  Necessarybutnotsufficient

•  Goal– Toiden,fyasetoftestcasessufficienttoexerciseallstatementsatleastonce

SoNwareEngineeringa.a.2008/2009White‐boxtes,ng 6

Page 7: White Box Testing

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

Page 8: White Box Testing

Statementcoveragecont.d

•  Possibleproblem– Eveninacaseofaprogramwithoutitera,ons,theexecu,onofeverystatementdoesnotguaranteethatallpossiblepathsareexercised

•  Possiblenega,veconsequence:–  Inpreviousjavaexampleprogramtheerrorofdivisionby0isnotiden,fied(statementD)

SoNwareEngineeringa.a.2008/2009White‐boxtes,ng 8

Page 9: White Box Testing

Decisioncoverage

•  Properlyincludesstatementcoverage•  Viceversadoesnothold•  Focusonallpossibledecisionsinaprogram•  Decisionsareinstatements

–  if,switch,while,for,do•  Goal

– Toiden,fyasetoftestcasessufficientforguaranteeingthateachdecisionwillhavevalue“true”atleastonceandvalue“false”atleastonce

SoNwareEngineeringa.a.2008/2009White‐boxtes,ng 9

Page 10: White Box Testing

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

Page 11: White Box Testing

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?

Page 12: White Box Testing

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

Page 13: White Box Testing

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

Page 14: White Box Testing

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);

Page 15: White Box Testing

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

Page 16: White Box Testing

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

Page 17: White Box Testing

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);

Page 18: White Box Testing

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

Page 19: White Box Testing

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

Page 20: White Box Testing

Methodologicalissue

•  Therearedifferentcombina,onoftestcasesthatguaranteedecisionandcondi,oncoveragebutitisnotalwaystrivialtofindone

•  Weneedasimpleandeffec,vemethodforiden,fyingtestcasesthatcoverbothdecisionsandcondi,ons

SoNwareEngineeringa.a.2008/2009White‐boxtes,ng 20

Page 21: White Box Testing

Controlflowgraph’spathscoverage

•  Basedon– Criteriaofdecisionandcondi,oncoverage– Program’scontrolflowgraph

•  Goal– Toprovideasimplemethodforguaranteeingdecisionandcondi,oncoverage

SoNwareEngineeringa.a.2008/2009White‐boxtes,ng 21

Page 22: White Box Testing

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)

Page 23: White Box Testing

Controlflowgraph

SoNwareEngineeringa.a.2008/2009White‐boxtes,ng 23

Sequenceofstatements

Simpleif

un,l

whilecase

Page 24: White Box Testing

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);

Page 25: White Box Testing

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

Page 26: White Box Testing

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);

Page 27: White Box Testing

Controlflowgraph,condi,onsanddecisions

•  Booleanvalues(“true”and“false”)associatedwithcondi,onsanddecisionshavecorrepondingarcs

•  Todesignasetoftestcasessuchthatallarcsofthecontrolflowgrapharetraversedimpliescondi,onanddecisioncoverage

SoNwareEngineeringa.a.2008/2009White‐boxtes,ng 27

Page 28: White Box Testing

Pathsofthecontrolflowgraph

•  Thesetoftestcasescorrespondstoasetofpathssuchthatalleveryarcistraversedatleastonce

•  Thenumberofpathssufficientforcoveringallarcsisalwaysequalorlessthanthecycloma,ccomplexity

SoNwareEngineeringa.a.2008/2009White‐boxtes,ng 28

Page 29: White Box Testing

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

Page 30: White Box Testing

Howtochoosepaths

•  Pragma,crule–  Experimentsshowthatthethenumberoferrors

increasewithincreasingofthecycloma,ccomplexity

–  Chooseanumberofpathsequaltothecycloma,ccomplexity

–  Youhaveapropor,onbetweennumberoftestcasesandcomplexityofthecode

SoNwareEngineeringa.a.2008/2009White‐boxtes,ng 30

Page 31: White Box Testing

Linearlyindependentpaths

•  Agoodcriterionforchoosingpathsisbasedontheconceptoflinearindependency

•  Amaximalsetoflinearlyindependentpathsiscalledabase–  ItisNOTunique

•  Abasecontainsanumberofpathsequaltothecycloma,ccomplexity

•  Intui,vely,everypathinput‐outputcanbeobtainedasalinearcombina,onofpathsofabase

•  Bychoosingabase,acertainextendofreliabilityidguaranteedwithrespectofcombina,onsoferrorsthathideeachother

SoNwareEngineeringa.a.2008/2009White‐boxtes,ng 31

Page 32: White Box Testing

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

Page 33: White Box Testing

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

Page 34: White Box Testing

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

Page 35: White Box Testing

Addi,onalconsidera,ons

•  Pathscoveragemethoddoesnotconsiderexplicitlyloopsitera,ons

•  Insomecasesitisnecessarytoperformitera,onsinordertotraverseallarcs

•  Itmightbeinteres,ngtoconsidersomeitera,oninanycasebutthenumberoftestcasesincreasesexponen,ally

•  Itisimportanttodecidethetypeofitera,ons

SoNwareEngineeringa.a.2008/2009White‐boxtes,ng 35

Page 36: White Box Testing

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

Page 37: White Box Testing

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)