公司現在對單元測試越發的看重,這對減少上線之后bug的減少會帶來很好的幫助,在筆者就職的公司里,在編寫代碼階段,開發人員需要做如下一些事情:
1、checkstyle統一規范代碼的格式,注釋的數量。
2、junit進行代碼的單元測試,并且在覆蓋率上面有比較高的要求。
3、dailybuild結合上述兩者并且外加findbugs。
4、除此之外,為了防止提交不合法的代碼到SVN倉庫,又做了SVN的控制,特意開發了SVN hooks,如果代碼不符合checkstyle和單元測試,以及findbugs等要求,都會提交代碼失敗。
很開心項目組的這些工作我都有參與進去進行設計和進行具體的開發,今天就分享一下我寫的一個很小的demo,主要是針對ant+junit進行單元測試以及生成測試報告,并且利用jacoco進行單元測試覆蓋率的報告,其中ant+junit的組合想必很多人已經非常的熟悉了~這里就不再贅述,jacoco是一個比較優秀以及很強大的工具,如果大家有興趣可以看看jacoco順便研究一下sonar+jacoco+maven的組合。 jacoco的下載地址為:http://www.eclemma.org/jacoco/
在本文中,我們就一個很小的工程進行一下上述三者的組合,讓大家體驗一下如何進行測試報告以及覆蓋報告的生成。
1.build.properties文件
在開始編寫build.xml之前,我們先定義一下build.properties文件
<span style="font-size:18px;">#Mon, 25 Nov 2013 12:08:58 +0800 src.dir=src test.src.dir=test build.dir=build build.classes=${build.dir}/classes build.test.dir=${build.dir}/test build.test.classes=${build.test.dir}/classes build.test.report=${build.dir}/report build.test.report.coverage=${build.test.report}/coverage junit.lib.dir=devlib </span> |
2.build.xml文件
<span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?> <!-- ====================================================================== Nov 25, 2013 11:51:47 AM project description clouder ====================================================================== --> <project name="build" default="init" basedir="." xmlns:jacoco="antlib:org.jacoco.ant"> <taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml"> <classpath path="${basedir}/devlib/jacocoant.jar"> </classpath> </taskdef> <property file="build.properties"> </property> <path id="compile-test-path"> <pathelement location="${build.classes}" /> <fileset dir="${junit.lib.dir}"> <include name="**/*.jar" /> </fileset> </path> <path id="run-test-path"> <path refid="compile-test-path" /> <pathelement location="${build.test.classes}" /> </path> <target name="init" depends="clean"> <mkdir dir="${build.dir}" /> <mkdir dir="${build.classes}" /> <mkdir dir="${build.test.dir}" /> <mkdir dir="${build.test.classes}" /> <mkdir dir="${build.test.report}" /> <mkdir dir="${build.test.report.coverage}" /> </target> <target name="clean"> <delete dir="${build.dir}"> </delete> </target> <target name="src_compile" depends="init"> <javac srcdir="${src.dir}" destdir="${build.classes}" failonerror="true" verbose="true"> </javac> </target> <target name="test_compile" depends="src_compile"> <javac srcdir="${test.src.dir}" destdir="${build.test.classes}" failonerror="true" verbose="true"> <classpath refid="compile-test-path"> </classpath> </javac> </target> <target name="coverage"> <jacoco:coverage> <junit haltonfailure="false" fork="true" printsummary="true"> <classpath refid="run-test-path"> </classpath> <formatter type="xml" /> <batchtest todir="${build.test.report}"> <fileset dir="${build.test.classes}" includes="**/*Test*.class"> </fileset> </batchtest> </junit> </jacoco:coverage> </target> <target name="run-test" depends="test_compile"> <junit haltonfailure="false" fork="true" printsummary="true"> <classpath refid="run-test-path"> </classpath> <formatter type="xml" /> <batchtest todir="${build.test.report}"> <fileset dir="${build.test.classes}" includes="**/*Test*.class"> </fileset> </batchtest> </junit> </target> <target name="test-report" depends="run-test"> <junitreport todir="${build.test.report}"> <fileset dir="${build.test.report}"> <include name="TEST-*.xml" /> </fileset> <report format="frames" todir="${build.test.report}/html" /> </junitreport> </target> <target name="coverreport" depends="coverage"> <jacoco:report> <executiondata> <file file="${basedir}/jacoco.exec" /> </executiondata> <structure name="MeltingPot unit test coverage rate report."> <classfiles> <fileset dir="${build.classes}" /> </classfiles> <sourcefiles encoding="UTF-8"> <fileset dir="${src.dir}" /> </sourcefiles> </structure> <html footer="Onecloud MeltingPot." destdir="${build.test.report.coverage}" /> <csv destfile="${build.test.report.coverage}/coverage-report.csv" /> </jacoco:report> </target> <target name="all-report" depends="coverreport,test-report"> <echo>generate unit test report and coverage rate report.</echo> </target> </project> </span> |
原文轉自:http://www.uml.org.cn/Test/201402143.asp