使用Cobertura,JUnit,HSQLDB,JPA涵盖您的测试
今天讓我們談?wù)勔粋€(gè)非常有用的工具,名為“ Cobertura”。 該框架與我們在另一篇文章中看到的Emma框架具有相同的功能。
Cobertura和Emma之間的主要區(qū)別在于Cobertura顯示帶有圖形的簡歷頁面。
如果要查看有關(guān)該主題的其他主題,請單擊以下鏈接: 使用JUnit Ant和Emma進(jìn)行的測試覆蓋 // // 使用HSQLDB,JPA和Hibernate進(jìn)行的JUnit // TDD –第一步 。
我將使用本文中的相同代碼( 帶有HSQLDB,JPA和Hibernate的JUnit ); 如果您想設(shè)置一個(gè)環(huán)境來運(yùn)行代碼,則可以按照在那里找到的步驟進(jìn)行操作(您將在本文末尾找到要下載的源代碼)。
我不是熟練的Ant用戶,您可能會看到一些代碼并認(rèn)為“此腳本不好”。 隨意告訴我如何“升級”螞蟻代碼的想法。 ;)
讓我們下載Cobertura庫– Download1.9.4.1
下載完成后,將jar文件(/cobertura.jar、/lib/*包括asm,jakarta,log4j)放入我們項(xiàng)目的lib文件夾中。
在我們項(xiàng)目的根源中,使用以下代碼創(chuàng)建一個(gè)名為“ build.xml”的文件 ( 創(chuàng)建的文件必須位于您項(xiàng)目的根目錄中,否則您的項(xiàng)目將無法使用 ):
<project name="Cobertura Coverage" basedir="."><!-- Project Source Code --><property name="src.dir" value="src" /><property name="build.dir" value="bin" /><property name="teste.dir" value="src/test" /><property name="lib.dir" value="lib" /><property name="report.dir" value="cobertura" /><!-- Project classpath --><path id="project.classpath"><pathelement location="${bin.dir}" /><fileset dir="${lib.dir}"><include name="*.jar" /></fileset></path><!-- Tested Class --><property name="DogFacadeTest" value="test.com.facade.DogFacadeTest" /></project>在上面的代碼中,我們正在創(chuàng)建源代碼和庫的路徑。 讓我們創(chuàng)建一個(gè)任務(wù),以刪除由Cobertura生成的文件和已編譯的Java源代碼。
<!-- Clears the paths --> <target name="01-CleannUp" description="Remove all generated files."><delete dir="${build.dir}" /><delete file="cobertura.ser" /><delete dir="${report.dir}" /><mkdir dir="${build.dir}" /><mkdir dir="${report.dir}" /> </target>要編譯我們的源代碼,請?jiān)谙旅嫣砑哟a并運(yùn)行任務(wù)(確保代碼可以編譯):
<!-- Compiles the Java code --> <target name="02-Compile" depends="01-CleannUp" description="invoke compiler"><javac debug="true" debuglevel="vars,lines,source" srcdir="${src.dir}" destdir="${build.dir}"><classpath refid="project.classpath" /></javac><copy file="${src.dir}/META-INF/persistence.xml" todir="${build.dir}/META-INF" /> </target>讓我們設(shè)置Cobertura,以便它可以測試測試類并準(zhǔn)備好環(huán)境:
<!-- Cobertura configs --> <property name="cobertura.instrumented-classes.dir" value="${report.dir}/instrumented-classes" /> <property name="cobertura.data.file" value="cobertura.ser" /> <path id="cobertura.classpath"><fileset dir="${lib.dir}" includes="/*.jar" /> </path><!-- Points to the cobertura jar --> <taskdef classpath="${lib.dir}/cobertura.jar" resource="tasks.properties" classpathref="cobertura.classpath" /><!-- Instruments the classes --> <target name="03-Instrument" depends="02-Compile"><delete quiet="false" failonerror="false"><fileset dir="${cobertura.instrumented-classes.dir}" /></delete><delete file="${cobertura.data.file}" /><cobertura-instrument todir="${cobertura.instrumented-classes.dir}"><fileset dir="${build.dir}"><include name="**/*.class" /><exclude name="**/*Test.class" /></fileset></cobertura-instrument><copy todir="${cobertura.instrumented-classes.dir}"><fileset dir="${src.dir}" casesensitive="yes"><patternset id="resources.ps" /></fileset></copy> </target>添加下面的代碼,您將能夠通過ant使用JUnit執(zhí)行測試:
<!-- Set up the instrumented classes path --> <path id="cover-test.classpath"><fileset dir="${lib.dir}" includes="**/*.jar" /><pathelement location="${cobertura.instrumented-classes.dir}" /><pathelement location="${build.dir}" /> </path><!-- Run the JUnit test --> <target name="04-RunTest" depends="03-Instrument" ><junit printsummary="yes" haltonerror="no" haltonfailure="no" fork="yes"><batchtest><fileset dir="${build.dir}" includes="**/*Test.class" /></batchtest><classpath refid="cover-test.classpath" /></junit><delete file="transaction.log" /> </target>作為我們的最后一個(gè)動作,讓我們使用Cobertura創(chuàng)建報(bào)告。 將下面的代碼添加到“ build.xml”中并執(zhí)行任務(wù):
<!-- Creates the Cobertura report --> <target name="00-CreateReport" depends="04-RunTest"><cobertura-report srcdir="${cobertura.data.file}" destdir="${report.dir}"><fileset dir="${src.dir}"><include name="**/*.java" /></fileset></cobertura-report><delete dir="${report.dir}/instrumented-classes" /><delete file="cobertura.ser" /> </target>更新您的Eclipse項(xiàng)目(在項(xiàng)目中按F5鍵),您將看到報(bào)告創(chuàng)建成功。 打開“ cobertura”文件夾中的“ index.html”文件。
Cobertura框架可幫助我們計(jì)算應(yīng)測試方法的次數(shù)。 在編寫家庭作業(yè)“ Dog equals”方法時(shí),請創(chuàng)建報(bào)告,這樣您可能會發(fā)現(xiàn)自己的“ equals”未涵蓋在內(nèi)。 創(chuàng)建測試并再次運(yùn)行報(bào)告。
您可以在此處從我們的項(xiàng)目中下載源代碼 。
如果您有任何疑問或疑問,請?jiān)谙旅鎸懴隆?
再見。 \ o_
參考:我們的JCG合作伙伴 提供的Cobertura,JUnit,HSQLDB,JPA涵蓋了您的測試 ? uaiHebert博客上的Hebert Coelho。
翻譯自: https://www.javacodegeeks.com/2012/02/covering-your-tests-with-cobertura.html
總結(jié)
以上是生活随笔為你收集整理的使用Cobertura,JUnit,HSQLDB,JPA涵盖您的测试的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用Apache ActiveMQ的JM
- 下一篇: wps照片滚动播放器(把很多张照片做成可