Tải bản đầy đủ - 0 (trang)
Chapter 12. Maximizing Test Coverage with Cobertura

Chapter 12. Maximizing Test Coverage with Cobertura

Tải bản đầy đủ - 0trang

executescorrectly(itiseasyenoughtowriteunitteststhat

exerciseanentireclasswithouttestinganybusinesslogicat

all!),inpracticeitisalwayspreferabletominimizetheamount

ofcodethatisnottestedatall.

Cobertura[46]isafree,opensourcetestcoveragetoolforJava.

Coberturaworksbyinstrumentingthecompiledbytecodefrom

yourapplication,insertingcodetodetectandlogwhichlines

haveandhavenotbeenexecutedduringtheunittests.Yourun

yourunittestsnormally,andtheinsertedbytecodelogsthe

executiondetails.Finally,usingtheselogs,Coberturagenerates

clear,readabletestcoveragereportsinHTML.Thesereports

provideahigh-leveloverviewoftestcoveragestatisticsacross

theentireproject,andalsoletyoudrilldownintoindividual

packagesandclasses,andinspectwhichlinesofcodewereand

werenotexecuted,allowingdeveloperstocorrectorcomplete

unittestsaccordingly.Coberturaalsomeasurescomplexity

metricssuchasMcCabecyclomaticcodecomplexity.

[46]http://cobertura.sf.net



CoberturaintegrateswellwithbothAntandMaven.Itcanalso

beexecuteddirectlyfromthecommandline,thoughthisisa

prettylow-levelstuffandshouldreallyonlybedoneifyoudon't

haveanyotherchoice.Atthetimeofthiswriting,noIDEplugins(forEclipse,NetBeans,oranyotherJavaIDE)were

available:IDEintegrationisanareawhereCoberturalags

behindthemaincommercialcodecoveragetoolssuchasClover

andCobertura'scommercialcousin,JCoverage.



Chapter12.MaximizingTestCoverage

withCobertura

TestCoverage

RunningCoberturafromAnt

CheckingtheCodeCoverageofTestNGTests

InterpretingtheCoberturaReport

EnforcingHighCodeCoverage

GeneratingCoberturaReportsinMaven

IntegratingCoverageTestsintotheMavenBuildProcess

CodeCoverageinEclipse

Conclusion



12.1.TestCoverage

Unittestingisrecognizedasacrucialpartofmodernsoftware

developmentpractices.Nevertheless,foranumberofreasons

discussedatlengthelsewhere,itisoftendoneinsufficientlyand

poorly.Basically,therearetwomainthingsthatcanmakea

unittestineffective:itcanexecutethecodebuttestthe

businesslogicpoorlyornotatall,oritcanneglecttotestparts

ofthecode.Thefirstcaseisfairlyhardtodetectautomatically.

Inthischapterwewilllookatthesecondtypeofissue,whichis

thedomainoftestcoveragetools.

Itisfairlyclearthatifapartofyourcodeisn'tbeingexecuted

duringtheunittests,thenitisn'tbeingtested.Andthisisoften

aBadThing.Thisiswheretestcoveragetoolscomeit.Test

coveragetoolsobserveyourcodeduringunittests,recording

whichlineshavebeenexecuted(andthereforesubjecttoat

leastsometesting).Andalthoughthefactthatalineofcodeis

executedduringunittestsoffersabsolutelynoguaranteethatit



executescorrectly(itiseasyenoughtowriteunitteststhat

exerciseanentireclasswithouttestinganybusinesslogicat

all!),inpracticeitisalwayspreferabletominimizetheamount

ofcodethatisnottestedatall.

Cobertura[46]isafree,opensourcetestcoveragetoolforJava.

Coberturaworksbyinstrumentingthecompiledbytecodefrom

yourapplication,insertingcodetodetectandlogwhichlines

haveandhavenotbeenexecutedduringtheunittests.Yourun

yourunittestsnormally,andtheinsertedbytecodelogsthe

executiondetails.Finally,usingtheselogs,Coberturagenerates

clear,readabletestcoveragereportsinHTML.Thesereports

provideahigh-leveloverviewoftestcoveragestatisticsacross

theentireproject,andalsoletyoudrilldownintoindividual

packagesandclasses,andinspectwhichlinesofcodewereand

werenotexecuted,allowingdeveloperstocorrectorcomplete

unittestsaccordingly.Coberturaalsomeasurescomplexity

metricssuchasMcCabecyclomaticcodecomplexity.

[46]http://cobertura.sf.net



CoberturaintegrateswellwithbothAntandMaven.Itcanalso

beexecuteddirectlyfromthecommandline,thoughthisisa

prettylow-levelstuffandshouldreallyonlybedoneifyoudon't

haveanyotherchoice.Atthetimeofthiswriting,noIDEplugins(forEclipse,NetBeans,oranyotherJavaIDE)were

available:IDEintegrationisanareawhereCoberturalags

behindthemaincommercialcodecoveragetoolssuchasClover

andCobertura'scommercialcousin,JCoverage.



12.2.RunningCoberturafromAnt

CoberturaintegrateswellwithAnt:withalittleconfiguration,

youcanhaveallthepowerandflexibilityofthetoolatyour

fingertips.Let'slookathowtointegrateCoberturaintoanAnt

project.

Firstofall,youneedtoinstallCobertura.Justdownloadthe

latestdistributionfromtheCoberturawebsite[*]andextractit

intoanappropriatedirectory.Onmymachine,Iinstalled

Coberturainto/usr/local/tools/cobertura-1.8,andaddeda

symboliclinkcalled/usr/local/tools/cobertura.

[*]http://cobertura.sourceforge.net/download.html



CoberturacomesbundledwithanAnttask.Youjustneedto

definethistaskinyourbuild.xmlfileasfollows:





















Thenextstepistoinstrumentyourfiles.Youcandothisusing

thecobertura-instrumenttask:

CodeView:

















































Thistaskisfairlysimple.Itisgoodpracticetoplacethe

instrumentedclassesintoadifferentdirectorythanthenormal

compiledclasses.Inthiscase,wegeneratetheminthe

instrumented-classesdirectory,usingthetodiroption.

Coberturastoresmetadataaboutyourclassesinaspecialfile,

calledbydefaultcobertura.ser.Here,weusethedatafileoption

toavoidanyconfusion(wewillneedtorefertoexactlythe

samemetadatafilewhenwegeneratethereports).Thisfileis

updatedwithexecutiondetailsduringthetestruns,andused

thentogeneratethereports.Tobesurethattheresultsare

reliable,wedeletethisfilebeforeinstrumentingthefiles.

Theactualclassestobeinstrumentedarespecifiedusinga

standardAntfileset.Notethat,forbestresultswithCobertura,

youshouldactivateline-leveldebuggingwhenyoucompileyour

Javaclasses.InAnt,youcandothisbyusingthedebugand

debuglevelattributes,asshownhere:


srcdir="${java.src}"

destdir="${java.classes}"

includes="**/*.java"

debug="true"

debuglevel="lines,source">







Nowcallthistargettomakesureeverythingworkssofar.You

shouldgetsomethinglikethis:



$antinstrument

...

instrument:

[mkdir]Createddir:/home/john/dev/commons-lang-2.2-src/ta

instrumented-classes

[delete]Deleting:/home/john/dev/commons-lang-2.2-src/cober

[cobertura-instrument]Cobertura1.8-GNUGPLLicense(NOWARR

SeeCOPYRIGHTfile

[cobertura-instrument]Instrumenting123filesto

/home/john/dev/commons-lang-2.2-src/target/instrumented-classes

[cobertura-instrument]Cobertura:Savedinformationon123clas

[cobertura-instrument]Instrumenttime:1371ms

Youshouldnowrunyourunittests(almost)normally.Well,not

quite.Infact,youneedtomodifyyourJUnittasksafairbitto

getthingsworkingproperly.BecauseCoberturateststake

considerablylongertorunthannormaltests,itisactuallya

goodideatowriteaseparatetargetexclusivelyforcoverage

tests.Inthisexample,wedefineatargetcalled"test.coverage"

thatinstrumentsthecode,andcompilesandrunstheunittests

againsttheinstrumentedcode:





haltonerror="${test.failonerror}">




file="${basedir}/cobertura.ser"/>




















Thereareafewimportantthingstonotehere.First,for

technicalreasonsrelatedtothewayCoberturageneratesits

datafiles,theforkoptionintheJUnittaskmustbesettotrue.

Second,youneedtoindicatethelocationofthecobertura.ser

fileinthenet.sourceforge.cobertura.datafilesystemproperty,

usingthesyspropertytask.Finally,youneedtomakesurethat

theclasspathcontainstheinstrumentedclasses(infirst

position)aswellasthenormaltestclasses,andalsothe

Coberturaclasses.

Runningthistargetshouldproducesomethinglikethe

following:

$anttest.coverage

...

test.coverage:

[junit]Runningorg.apache.commons.lang.LangTestSuite



[junit]Testsrun:635,Failures:0,Errors:0,Timeelapse

[junit]Cobertura:Loadedinformationon123classes.

[junit]Cobertura:Savedinformationon123classes.

Nowwecangettotheinterestingstuffandgeneratethe

Coberturareport:

CodeView:




...






destdir="${coveragereport.dir}"

srcdir="src"

datafile="${basedir}/cobertura.ser"/>





























Thiswillgenerateasetofreportsinthe

${build.dir}/reports/coberturadirectory,illustratedinFigure

12-1.Coberturareportsarefairlyintuitive,especiallyifyou

haveworkedwithothercodecoveragetools.Wewilllookat

someofthefinerpointsofhowtointerpretaCoberturareport

inSection12.4.

Figure12-1.ACoberturareport



IfyourCoberturareportsaregoingtobeusedbyanothertool

(suchastheHudsonContinuousIntegrationserver(seeSection

8.16),youalsowillneedtogenerateyourreportsinXML

format.Youdothisasfollows:

CodeView:




...






destdir="${coveragereport.dir}"



srcdir="${source.home}"

datafile="${basedir}/cobertura.ser"/>



























12.3.CheckingtheCodeCoverageof

TestNGTests

TestNG(seeChapter11)isaninnovativeandflexible

annotation-basedtestingframeworkthataimsatovercoming

manyofthelimitationsofJUnit.Here,welookathowtouse

CoberturatomeasuretestcoverageonTestNGtests.The

techniquepresentedherewasinitiallydescribedbyAndy

Glover.[*]

[*]http://www-128.ibm.com/developerworks/forums/dw_thread.jsp?



forum=812&thread=110765&cat=10



CoberturaisnotlimitedtomeasuringtestcoverageonJUnitbasedtests.Indeed,itcanbeusedtomeasuretestcoverage

evenifyouareusingotherunittestingframeworkssuchas

TestNG.RunningTestNGwithCoberturaisarelativelysimple

task.First,youneedtodefinetheCoberturatask,and

instrumentyourclassesinthenormalway,usingthecoberturainstrumenttask(seeSection12.2).Thiscodeislistedhere

againforconvenience:

CodeView:



...































datafile="${basedir}/cobertura.ser">





































Next,insteadofrunningyourtestsusingtheJUnittask,you

needtousetheTestNGtaskinstead.Therearetwothingsto

rememberhere.First,youneedtoprovidea

containingtheinstrumentedclasses,thetestclasses,andthe

Coberturalibraries.Second,youneedtospecifya

elementthatprovidesthepathoftheCobertura

datafile(cobertura.ser).Atypicalexample,whichrunsallthe

TestNGclassesintheproject,isshownhere:

















file="${basedir}/cobertura.ser"/>








Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Chapter 12. Maximizing Test Coverage with Cobertura

Tải bản đầy đủ ngay(0 tr)

×