Design for Testability

发布时间:   来源:文档文库   
字号:
Abstract
DesignforTestability

StefanJungmayr
Testabilityisasoftwarequalitycharacteristicthatisofmajorrelevancefortestcostsandsoft-waredependability.Still,testabilityisnotanexplicitfocusintoday’sindustrialsoftwaredevel-opmentprojects.Relatedprocesses,guidelines,andtoolsaremissing.
Thispaperisaboutdesignfortestability,themainintersectionofsoftwaredesignandtesting.Wedescribe1elementsofobject-orientedsoftwaredesignthatmaycausetestingproblemsand2guidelinesonhowtoimprovetestabilityincludingtheuseofdesignpatterns.Thespe-cialfocusisonsystemdependenciesandobservability.
Keywords
testing,testability,designfortestability,designpatterns,dependencies,observability
CONQUEST2002-57

1Introduction
Testingconsumesasignificantamountoftimeandeffortwithinanaveragesoftwaredevelopmentproject.Therearedifferentapproachestokeeptestcostsundercontrolandtoincreasethequalityoftheproductundertest:
improvethesoftwarespecificationanddocumentation,reduceorchangefunctionalrequirementstoeasetesting,usebettertesttechniques,usebettertesttools,improvethetestprocess,trainpeople,and
improvethesoftwaredesignandimplementation.
Inthispaperwefocusonsoftwaredesignandhowitcanbeimprovedtofacilitatetesting.
Thedegreetowhichasoftwaresystemorcomponentfacilitatestestingiscalledtestability.Designingasoftwaresystemwithtestinginmindiscalleddesignfortestability.
Therearedifferentwaystoimprovethetestabilityofasoftwaredesign,forexampletolimittheuseofclassinheritance,tolimitthestatebehaviorofobjects,toavoidoverlycomplexclasses,toavoidnon-determinism,andtopreparegraphicaluserinterfacesfortheuseoftestautomationtools.Inthispa-perweconcentrateonsystemdependenciesandobservability,describerelatedtestingproblems,andgiveguidelinesonhowtoimprovetestability.Weassumethatthereaderissomehowfamiliarwithdesignpatterns[Gamm94]andrefermainlytoJAVAasanunderlyingprogramminglanguage.
2Dependencies
Thestaticbuildingblocksofobject-orientedsoftwaresystemsareclassesandinterfaces.Aclassmayimplementoneormoreinterfaces.Everyclassandinterfacedefinesatype.Betweenaclass/interfaceandanotherclass/interfacetheremaybeadependency.Dependenciesarenecessaryforfunctionalitybutmaketestingmoredifficult.Sometypesofdependenciesareespeciallyproblematicfortestingandwillbedescribedinthefollowingsections,togetherwithguidelinesonhowtorefactorthem.
IfaclassAdependsonanotherclassBtofulfillitsfunctionalityAiscalledaclientclassandBiscalledaserverclass.
2.1Hard-WiredDependenciesonClasses
Context:Adependencyonaserverclassishard-wirediftheserverclasscannotbesubstitutedbysomeotherclass(e.g.asubclassoftheserverclasswithoutchangingthesourcecodeoftheclientclass.AclassAishard-wiredtoclassBifamethodofA
callsoneoftheconstructorsofclassB,and/oraccessesastaticmethodorfieldofclassB.
Anexampleforanaccesstoastaticmethod:ClassBimplementstheSingletonpattern[Gamm94],amethodofclassAcallsthestaticmethodB.getInstance(.
58CONQUEST2002

Problem:Duringunittestwewanttotestaclassinisolation.Thereforewewanttosubstituteitsserverclassesbystubsormockobjects[Link01].Hard-wireddependenciestoserverclasseshinderthis.Similarproblemsariseduringintegrationtestingifaserverclassshouldbestubbedbecauseitisnottest-ready.
Ahard-wireddependencytoaparticularclassisespeciallyproblematicifitmanifestsinmanydifferentcodelocations.
Solution:Therearedifferentwaystobreakhard-wireddependenciesonclasses:
Makeanotherclassresponsibletoestablishthelinkbetweentheclientandtheserver.Addapa-rameteroftheservertypetoamethodoftheclientclass(aconstructor,oramethodthatrequiresaccesstoaserverinstance,oradedicatedsetupmethodwhichallowsotherobjectstosetthelink.
Iftherearemanyserverclassesthisapproachwouldresultintoomanyparameters.Avariationofthisapproachisthereforetouseacontextobject[Rain01](whichencapsulatesandprovidesac-cesstoanumberofserverinstancesasaparameter.
Duringtestingtheparameter(scanbeusedtoestablishalinktoastubinstead.
Makeanotherclassresponsibletoestablishalinkfromtheclienttoaninstanceofafactoryclass[Gamm94]whichallowstheclienttogetaccesstoanobjectimplementingtheservertype.Iftheserverclassisasingletonthefactoryclassalwaysreturnsareferencetotheonlyinstanceofthesingleton.
Duringtestingthefactoryclasscanreturnastubinstead.
Theclientclassonlyknowsthetypeoftheserverclass.Thelinktooneofitsinstancesissetatrun-timebyreadingaconfigurationfileorsearchingaparticulardirectoryofthefilesystemforex-ample.
Assigningtheresponsibilityforobjectcreationandconnectionofobjectstodifferentclassesfollowstheprinciple"separationofconcerns"whichingeneralimprovestestability.
Consequences:Ifaclientclass(isnothard-wiredtoaserverclassbutcanpotentiallycollaboratewithmanyclassesimplementingtheservertypemoreclient-servercombinationshavetobetestedduringintegrationtesting.Incaseofdynamicdependenciestheconcreteserverclassesmaybeun-knownatthetimeoftestingwhicheliminatestheconfidenceusuallydrawnfromintegrationtesting[Szyp97].Thereforeitbecomesmoreimportanttoassuretypeconformanceoftheserverclassesduringdesignandimplementation.
2.2Hard-WiredDependenciestoSystemResources
Context:Adependencytoasystemresourceishard-wiredifthesystemresourcecannotbeex-changedwithoutchangingthesourcecodeoftheclientclass.Hard-wireddependenciestosystemresourcesarecausedforexampleby
explicitdeclarationsoffilenamesinprogramstatementslikenewFile("C:/dir/filename.ext"or
accesstostandardoutput/inputstreamslikeSystem.out.println("...".
Problem:Hard-wireddependenciestosystemresourcesmayhindertestingiftheseresourcesareunavailableornotsuitablefortestingandnootherresourcecanbeusedinstead.
Solution:Thesolutionsaresimilartothesolutionsforhard-wireddependenciesonclasses.Addition-allyconsidertouseaspecializedclassthathandlesinput/outputoperationsinsteadofusingfilesorstreams.Thisfacilitatesloggingandtheuseofmockobjects.
CONQUEST2002-59

2.3DependenciestoClasses
Problem:Ifaclientclassdependsonaserverclasswecanimplementastubasasubclassoftheserverclass.Implementingthestubasasubclassoftheserverclassisnotpossibleincasethat
thestubshouldinheritfromsomeother(testframeworkclass(butmultipleinheritanceisnotavailableinJavaor
theclassimplementstheSingletonpattern.Inthiscaseitcannotbesubclassedbecauseofitsprivateconstructor(asuper-calltotheconstructorisnotpossibleandbecauseofitsstatic
getInstance(methodwhichcannotbeoverridden(e.g.toreturnthestubinsteadofthesin-gletoninstance.
Solution:Leteverytestrelevantclassimplementacorrespondinginterface.Iftheclassispartofaninheritancetreetherootclassshallimplementaninterfaceoritshallbeanabstractclass(compare[Souz98],[McGr01].Eachclienthasonlydependenciestotheinterfacethattheserverimplementsortotheabstractsuperclassoftheserver.(Note:Incomponentbasedsystemsremoteobjectscanal-waysbeaccessedonlyusingtheirinterfaces.
Consequence:Itisnotpossibletodefinestaticmethods(likeconstructorswithininterfaces.There-foreitisnecessarytousetheFactorypatterntocreateinstances[Carm98].Usingafactoryclassforobjectcreationallowstheapplicationtokeeptrackofallinstantiatedobjects.Thiscanbeusedtomonitorresourceconsumptionandtoaiddebuggingduringtestingandmaintenance.
2.4HighCoupling
Problem:Theclassundertest(CUTdependsonalargenumberofotherclasses.Thismakestesttasksmoredifficult,forexamplethedefinitionandselectionoftestcases,thedefinitionofthetestorder,thecreationofstubs,test-setupandintegrationtesting.
Solution:Adheretothegeneralsoftwareengineeringprinciplesonhowtoreducecoupling.Limitthevisibilityofclasseswithinthesystembymakingthemprivateorprotectedwheneverappropriate.Pre-ferprimitiveandlanguagedefinedparametertypesinsteadofdeveloperdefinedclassesandinter-faces.UsetheMediatorpatterntoreducethenumberofclassestheCUThastointeractwith.
IfsomebasicfunctionalityoftheCUTcanbetestedbasedonasubsetoftheserverclassesdefineaconstructorwhichrefersonlytothissubset[McGr01].
2.5CyclicDependenciesbetweenClasses
Problem:Classeswithinadependencycyclecannotbetestedinisolation.Implementingstubstobreakdependencycyclescausesadditionaleffort.
Solution:Breakadependencycyclebyextractingtheinterdependentfunctionalityintoacommonserverclass(demotionorahigher-levelclass(escalation[Lako96].
AnotherpossibilityistobreakthedependencycyclebyusingtheObserverpattern[Gamm94](note:usingtheObserverpatterndoesnotbreakthecyclicdependencyontheinstancelevel.
60CONQUEST2002

2.6CyclicLinksbetweenObjects
Problem:Objectsarecoupledvialinks.Cycliclinksbetweenobjectsleadtotheproblemofre-entrance.Re-entrancemeans,thatamethodofanobjectisinvokedwhileanothermethodofthesameobjectisstillexecuting.Maintainingaconsistentobjectstateaswellasfindingandcorrectingerrorsbecomesdifficultinthepresenceofre-entrancesituations[Szyp97].
Solution:Preferhierarchicalstructuresofobjectlinks.Apollingmechanismmaybeapossiblesolu-tiontoavoidcyclicrelationships.IfcyclicrelationshipscannotbeavoidedusetheCommandpatternforclassinteraction.TheCommandpattern[Gamm94]decouplesmethodinvocationoftheclientandserverclassandfacilitatesloggingaswellasreplayofinvocationsequences.
2.7DenseObjectNetwork
Problem:Atree-likenetworkofobjectlinksispreferableintermsoftesting.Adensenetworkofobjectlinksmeansthatalargenumberofobjectsarereferencedbymorethanoneotherobject.Thisin-creasestheprobabilityofunwantedside-effects.Forexample:
Acalledmethodmayunintentionallychangethestateofitsparameters(Javadoesnotallowtodistinguishbetweencall-by-valueandcall-by-referenceparameters.
Accessormethodsmayreturnareferencetoan"internal"objectthatmightbechangeduninten-tionallybythecallinginstance.
Unknownsideeffectsmakeitmoredifficulttolocateerrors.Solution:Considerthefollowingtoavoiddenseobjectnetworks:
Callmethodswithcopiesofparameterobjectsiftheoriginalobjectsshallnotbechanged.Returnnewobjectsfromaccessormethods(i.e.methodswhichmustnotchangethestateofitsobject[Gran99].
Useimmutableobjectswhereverpossibletoavoidside-effectsincaseofsharedaccess[Lisk01].Declareattributesasfinalwhereverappropriate.
3Observability
Duringandaftertestexecutionitisimportantfortesterstobeabletoobservethereturnvalues,inter-mediatecomputationresults,thesequenceofmethodcalls,andtheoccurrenceoffaults.Theob-servabilityofobject-orientedsystemsoftenneedstobeimproved.
3.1InformationHiding
Problem:PrivateorprotectedattributesofaCUTcannotbeaccessedbythetestdrivertosetthestateoftheCUTbeforetestexecutionandtoobservethestateaftertestexecution.
Solution:Implementastandard-testinterface[Riel96][Pemm98]withineachclassincludingmethodslikeparse(andtoString(toreadandwriteobjectstateandamethodequal(forcom-parisonofobjects.
CONQUEST2002-61

3.2InformationLoss
Problem:Duringthetestofamethodintermediatecomputationresultsgetlost.Fromthereturnvalueandoutputparametervaluesaloneitmaynotbepossibletodetectaresultwhichiscorrectbychancedespiteanerrorinthemethodimplementation.
Solution:Toimproveobservabilityofintermediatecomputationresults
usetest-facilities(e.g.assertionswithinthemethodbodytoreportonwrongintermediateresultsorviolationsofpre-andpostconditionsandclassinvariants,
implementatestinterfacethatallowsatestclasstoregisterforinternaleventsoftheCUTwhichcarryinformationonintermediatecomputationresults(andothertestrelevantinformation,orusea(configurableloggingfacilitytologintermediatecomputationresults.
3.3UnobservableSequenceofMethodCalls
Problem:Thesequenceofthemethodcallsandtheactualparametervaluescannotbeobservedduringintegrationtestingwithoutusingdebuggersorinstrumentation.
Solution:UseapatternthatindirectsmethodcallsliketheAdaptor,Bridge,Facade,Proxy,orBrokerpattern.Thisallowstologthemethodcallsandparametervalues.(Note:Proxiescanbecreateddur-ingrun-timeusingthedynamicproxyfeatureavailablesinceJAVA1.3.
Consequences:Everyindirectionofmethodcallscausesrun-timeoverhead.Withindistributedappli-cationstheBrokerpatternpossiblymakesitmoredifficulttotestthecollaborationofclientsandserv-ers[Busc96].
3.4Non-LocalFaults
Problem:Afaultcrossesoneormorepackage/subsystemboundariesbeforeitmanifestsasafailure.Itisdifficulttolocatetheerrorunderlyingthefailurebecausethefailurecannotbeclearlyattributedtotheresponsiblepackage/subsystem.
Solution:Useadefensivedesignapproachforthepackage/subsysteminterface.Useafilterclasstoseparatetheimplementationoftheinterfacefromthecodethatshallpreventclientsfromissuingin-correctmethodcalls.Userdefinedexceptionsandruntimeexceptionsshouldbecatchedbeforetheyareabletocrosspackage/subsystemboundaries.
62CONQUEST2002

4Summary
Inthispaperwehavepresentedcharacteristicsofobject-orienteddesignthatarecriticalfortestingaswellasrelatedguidelinestoimprovetestability.Thiscollectionoftestproblemsandtestabilityguide-linescanbeusedasinputforcompanyspecificprogrammingguidelines,designreviews[Jung99],andthedesignofapplicationframeworks.
Ofcourse,testabilityhastobeconsideredthroughouttheentiresoftwaredevelopmentproject.Thismeanstostartwithtestablerequirementsaswellastestabilityrequirements,i.e.requirementsrelatedtothetestabilityofthesoftwareproduct.Stakeholdersfortestabilityrequirementsincludethecustom-ersandsoftwareuserssincetestabilityisimportanttoshortenmaintenancecyclesandtolocatere-sidualerrors.
Futureempiricalstudiesabouttheeffectsandeconomicsoftestabilityarenecessaryinordertoestab-lishtestabilityengineeringasanewdisciplineofsoftwareengineering.

CONQUEST2002-63

5References
[Arno94][Busc96][Carm98]
ThomasR.ArnoldandWilliamA.Fuson,"Testinginaperfectworld,"CommunicationsoftheACM,vol.37,pp.78-86,Sept.1994.
FrankBuschmann,RegineMeunier,HansRohnert,PeterSommerlad,andMichaelStal,"Pattern-OrientedSoftwareArchitecture,Volume1:ASystemofPatterns,"JohnWiley&Son,1996.
AndyCarmichael,"Applyinganalysispatternsinacomponentarchitecture,"TogetherSoftUKLtd,1998,URLhttp://www.togethersoft.co.uk/.
[Gamm94]ErichGamma,RichardHelm,RalphJohnson,andJohnVlissides,"DesignPatterns:Elementsof
ReusableObject-OrientedSoftware,"Addison-Wesley,1994.[Gran99][Jung99][Lako96][Lind95][Link01][Lisk01][McGr01]
MarkGrand,"PatternsinJava,"Volume2,JohnWiley&Sons,1999.
StefanJungmayr,"Reviewingsoftwareartifactsfortestability,"presentedatEuroSTAR99,Barcelona,Spain,Nov.8th-12th,1999.
JohnLakos,"Large-scaleC++softwaredesign,"Addison-Wesley,1996.
F.vanderLindenandJ.Mueller,"Creatingarchitectureswithbuildingblocks,"IEEESoftware,pp.51-60,Nov.1995.
JohannesLink,"EinsatzvonMock-ObjektenfürdenSoftwaretest,"JAVASpektrum,no.4,July/August2001,pp.53-59.
BarbaraLiskovandJohnGuttag,"ProgramdevelopmentinJAVA:abstraction,specification,andobject-orienteddesign,"Addison-Wesley,2001.
JohnD.McGregorandDavidA.Sykes,"PracticalGuidetoTestingObject-OrientedSoftware,"Addison-Wesley,2001.
[Pemm98]KameshPemmaraju,"Effectiveteststrategiesforenterprise-criticalapplications,"JavaReport,Dec.
1998.[Rain01][Riel96][Souz98][Szyp97]
J.B.Rainsberger,"Useyoursingletonswisely,"IBMdeveloperWorks,July2001,URLhttp://www.ibm.com/developerworks/.
ArthurJ.Riel,"Object-OrientedDesignHeuristics,"Addison-Wesley,1996DesmondD’SouzaandAlanWills,"Catalysis,"1998.
ClemensSzyperski,"ComponentSoftware.BeyondObject-OrientedProgramming,"Addison-Wesley,1997.
64CONQUEST2002

本文来源:https://www.2haoxitong.net/k/doc/35e2f5c458f5f61fb736660e.html

《Design for Testability.doc》
将本文的Word文档下载到电脑,方便收藏和打印
推荐度:
点击下载文档

文档为doc格式