Cobertura使用代碼覆蓋率學(xué)習(xí)筆記_第1頁
Cobertura使用代碼覆蓋率學(xué)習(xí)筆記_第2頁
Cobertura使用代碼覆蓋率學(xué)習(xí)筆記_第3頁
Cobertura使用代碼覆蓋率學(xué)習(xí)筆記_第4頁
Cobertura使用代碼覆蓋率學(xué)習(xí)筆記_第5頁
已閱讀5頁,還剩8頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、代碼覆蓋率1.代碼覆蓋率工具比較用過幾款java單體覆蓋率工具。稍稍列舉一下:Jcoverage優(yōu)點(diǎn):1支持ANT TASK2 支持 Eclipse Plugin缺點(diǎn)1商用軟件,要money的2貌似以前instrument內(nèi)存管理的很郁悶,輕松就會(huì) outofmemoryEmma優(yōu)點(diǎn):1開源軟件2 支持 ANT TASK缺點(diǎn):ANT TASK的參數(shù)指定較弱,coverage參數(shù)需要追加配置文件。Cobertura優(yōu)點(diǎn):1開源軟件2 支持 MARVEN缺點(diǎn):可定制參數(shù)太少從文件的輸出上來看,還是以xml,html生成為主。2.Codepro analytiX代碼覆蓋率安裝eclipse插件,在這

2、里不細(xì)說了。更新地址:CodePro AnalytiX - http:/dl.google.cOm/eclipse/inst/codepro/latest/3.6可以參考Eclipse常用插件圖解匯總.docx文檔在菜單選擇CodePro>Views>Code Coverage選擇統(tǒng)計(jì)結(jié)果視圖I???弟可 運(yùn)行(RSf寸雄質(zhì)4j ED(W)穗助(H)Cfieat Sh&etcViews*PerspectivesK言View LogClear LogmExplore WorkspacePreferencesAbout怛 Evaluation Ouide HelpWako me

3、每f, & & &9瓶AudHfAudit ExplorerMetricsDependenciesCede Coverage蠢TestCase OutlineSi mils rC ode在項(xiàng)目上點(diǎn)擊鼠標(biāo)右鍵,CodePro Tools>Instrument for Code Coverage如下圖所示,Exercise-KtjiHclIoDcb Jjabrefd k 吧 TTPe I rum l 口 fil 氏 xi 七 i_r 死 jBuild PathlSourceX5CSIRefactorX KTIA Import.,l3 Export.Build Proj

4、ect的 Refreshf$Close ProjectClose Unrelated ProjectsAssign Working Sets.Run Optimized AsRun With CloverAsRun As伊Debug AsValidateCloverTeamACompare With*Rest or* from Local History.CodePro ToolsConfigureienSum 2.z I Audit Code八 Audit Code L1 sing.|J Compute Metrics|馴 Compute Metrics Using,Ju Generate

5、Factory Clashes Generate Test Case?ijj Find Similar Codt國 Find Dead CodeV Check Spelling岷 Repair JavadocF Analyze Dependencies點(diǎn) Instruirtent fdr Code CeverigeProgrtW聘I & Explore正常運(yùn)行測試用例,在Code Coverage視圖會(huì)出現(xiàn)結(jié)果Java-Exercise J-MachOperators/src/math/Addlxtended.jivo-EdHpse /Usersuduynguyen/workspa.c

6、e-sv/at脂*3Plug-ir D. 邑妙皆審GH 念曲QTW 圈回 如衍爐import ja/a.uttl.LinkedList;遷 N 伽 J 23、o Finished artfir 0.143 seconds小 £31¥ 洋,今十JAdd.java闡 AddLxenmdJava 口publicRjns: 52/ a Errors Failuri Ifernaifi AdclTpit Runnpf HJ)i。:EittstSum 4EtcstSum 2Eit«5tSum_3 ELjmach.lestAII Runner; jUn t 4 SLinaih.

7、AddExlendedTest Runner faEmath.MvltiplyExterdedTc5t R以red: notcovered、ble sum (L inkedL ist<Double> nj(rbers)( =0;umbers itercrtor(); MextQ) LueD = CDouble>it. nextf);/result 4- valueD.doubleVolue();return result;public static double sum (Yector<Double> nurrbers) double result=0;Writa

8、bleSmart InsertDetdih HistoryClasses:Method!1 / 12 / 3100.0% 66.791Lines:if 1361.5%Blcxks:6/966.7%Inwruciions:22 / 4351 .家coverage details可 GonsoH IKCseraUon Results p Gode Coverage 云 1 Dtad God«= Failure TraceIfil Exercise-3-MathOotrators (51.4?6.由 nuth<51.4*. +2.2X) 21 人ddjgpo.g, 18A&

9、.) 2) Add 7)Ex ce nd edJB| )improvementsince last run使用CodePro AnalytiX.的優(yōu)點(diǎn):是Eclipse的集成插件,不需要想 Cobertura建立項(xiàng)目,簡 單容易上手,對提高代碼測試覆蓋率節(jié)省了事件。缺點(diǎn):無法過濾類,在報(bào)告中包含了整個(gè)測試項(xiàng)目的測試點(diǎn)類。因此生成報(bào)告需要借助丁Cobertura 來完成??偨Y(jié):使用CodePro AnalytiX來輔助編寫測試用例,達(dá)到質(zhì)量目標(biāo)。中創(chuàng)中問件的質(zhì)量目標(biāo)是: 行覆蓋率90%,分支覆蓋率75%。然后,使用Cobertura生成測試用例的報(bào)告。3.Cobertura 例子首先倉U建一個(gè)

10、Java 項(xiàng)目,項(xiàng)目 名: utmp.code.coverage.test結(jié)構(gòu)如下圖所示:J U utmp.eo dst &斯com.amstthew.prarti.eebertura7| Hellojavaj> 謠 test由 com amartheA, p racti s e, c c berturaT HelloTest.java0 MURE jdkl.5.0_16> M弓I用的庫j 口 build, m coberturajli b fe junit的 com.5pring&DLJJunit-4.4.-0jar菱 asnn-3r0.jair1

11、3&rn-tree3»Djar.jakdrta-oro -2.0.8.J arQ Jakarta-orodic&n&t,txt。I&g4j-1.2.9.jarj logij-licen&e.txt加obrtursjr國 perties栗buildml接下,創(chuàng)建一個(gè)Ant構(gòu)建配置文件,幫我們實(shí)現(xiàn)自動(dòng)化的編譯,單元測試以及cobertura來測量單元測試的代碼覆蓋情況。Ant的build.xml文件內(nèi)容如下:增加測試點(diǎn),在紅色1處添加,增加測試用例在紅色 2處添加。<?xml version="1.0"

12、encoding="UTF-8” ?><projectname= "cobertura.utmp.code.coverage"default= "coverage" basedir= "." ><!-弓I用perties文件配置路徑信息<propertyfile= "perties"/><!- 設(shè)置 cobertura 路徑-><pathid= "cobertura.classpath"<f

13、ilesetdir="$cobertura.dir"<includename= "cobertura.jar"/><includename= "lib/*/*.jar"/></fileset></path><!- 項(xiàng)目依賴的jar包-><!-path id="project.classpath"><fileset dir="$project.lib.dir"><include name="*.ja

14、r" /></fileset></path-><!- 配置 cobatura ant<taskdefclasspathref=擴(kuò)展任務(wù)->"cobertura.classpath"resource= "perties"/><targetname= ''init"><mkdirdir="$classes.dir"/><mkdirdir="$instrumented.dir"/>

15、;<mkdirdir="$reports.xml.dir"/><mkdirdir="$reports.html.dir"/><mkdirdir="$coverage.xml.dir"/><mkdirdir="$coverage.html.dir"/></target><!-編譯源代碼-><targetname= ''compile" depends= "init">-><!-

16、創(chuàng)建編譯結(jié)果目錄<javacsrcdir=debug= "true""$src.dir:$testsrc.dir"destdir= "$classes.dir"<!- classpath refid="project.classpath" / -><classpathrefid=</javac>"cobertura.classpath"/></target><targetname= "instrument"depen

17、ds= "init,compile"<!-Remove the coverage data file and any old instrumentation.-<deletefile= "cobertura.ser" /><deletedir="$instrumented.dir"/><!- Instrument the application classes,writing the instrumented classes into $(build.instrumented.dir).->&

18、lt;cobertura-instrumenttodir="$instrumented.dir"><!- The following line causes instrument to ignore any source line containing a reference to log4j,for the purposes of coverage reporting.-><!-排除的類-><ignoreregex= "org.apache.log4j.*”/><filesetdir= "$classes

19、.dir"><!- Instrument all the application classes, but don't instrument the test classes.-><!- 包含的類-><includename= "*/*.class"/><!- 1.添加測試點(diǎn)類,可以參考下面的寫法而不需要上面*/*.class -><!- includename="com/cvicse/inforguard/sp/userman/service/impl/UserManImpl.clas

20、s" / -><!- 排除的類-><excludename= "*/*Test.class"/></fileset></cobertura-instrument></target><!-單元測試-><targetname= "test" depends= "init,compile" ><junitfork= "yes" dir= "$basedir" failureProperty= &q

21、uot;test.failed" > <!-Note the classpath order: instrumented classes are before the original (uninstrumented) classes. This is important.-><classpathlocation= "$instrumented.dir"/><classpathlocation= "$classes.dir"/><!The instrumented classes referenc

22、e classes used by the Cobertura runtime, so Cobertura and its dependencies must be on your classpath.-><classpathrefid= "cobertura.classpath"/><!- classpath refid="project.classpath" / ->if= "testcase" /><formattertype= "xml" /><test

23、name= "$(testcase)" todir= "$(reports.xml.dir)'<batchtesttodir="$(reports.xml.dir)"unless="testcase"<filesetdir="$testsrc.dir"<includename="*/*Test.java"/><!- 2.增加測試用例類,可以參考下面的例子寫-><!-includename="com/cvicse/inforgu

24、ard/sp/userman/service/impl/UserManImplTest.java" / -></fileset></batchtest>>>/>"$reports.html.dir"/>totallinerate= "100" /></junit><junitreporttodir="$reports.xml.dir"<filesetdir= "$reports.xml.dir"<includenam

25、e= "TEST-*.xml"</fileset><reportformat= "frames" todir=</junitreport></target><targetname= "coverage-check" ><cobertura-checkbranchrate="34"</target><!- 生成coverage xml格式報(bào)告 -><targetname= "coverage-report"

26、><!-Generate an XML file containing the coverage data using the "srcdir" attribute.-><cobertura-reportsrcdir="$src.dir" destdir= "$coverage.xml.dir"format= "xml"/></target><!- 生成 coverage html格式報(bào)告-><targetname= "alternate-cov

27、erage-report"><!-Generate a series of HTML files containing the coveragedata in a user-readable form using nested source filesets.-><cobertura-reportdestdir="$coverage.html.dir"><filesetdir= "$src.dir" ><includename= "*/*.java"/></file

28、set></cobertura-report></target><!-清除編譯生成的結(jié)果文件-><targetname="clean" description="Remove all files created by the build/testprocess." ><deletedir="$classes.dir"/><deletedir="$instrumented.dir"/><deletedir="$reports

29、.dir"/><deletefile="cobertura.log"/><deletefile="cobertura.ser"/></target><targetname="coverage" depends="clean,init,compile,instrument,test,coverage-report,alternate-coverage-report”description="Compile, instrument ourself, run th

30、etests and generate JUnit and coverage reports."/></project>perties 文件:#The source code for the examples can be found in this directorysrc.dir= ./srctestsrc.dir= ./test#Test items to be dependent on the path of jar package#project.lib.dir=E:/workspace 7/sp_commonlib#The path t

31、o cobertura.jarcobertura.dir= cobertura#Classes generated by the javac compiler are deposited in this directory#classes.dir=./binclasses.dir= classes#Instrumented classes are deposited into this directoryinstrumented.dir= instrumented#All reports go into this directoryreports.dir= reports#Unit test reports from JUnit are deposited into this direct

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論