java

位置:IT落伍者 >> java >> 浏览文章

Java应用利器组合:Ant+JUnit+Cobertura


发布日期:2022年11月02日
 
Java应用利器组合:Ant+JUnit+Cobertura

看标题就知道这个是开发一个Java应用的利器组合使用Ant完成工程的构建(Build)使用JUnit完成单元测试使用Cobertura完成代码覆盖测试也可以辅助查找性能瓶颈和一些类型的BUG下面是一个完整的buildxml范例可以完全拿来用不需任何修改只要你的目录和这里的目录一致(应该也是很通用的)

下载下面的buildxml文件

文件内容

<project default=all>

<!– =================================================================== –>

<!– Definitions –>

<!– =================================================================== –>

<property name=basedir location= />

<property name=libdir location=${basedir}/lib />

<property name=srcdir location=${basedir}/src />

<property name=testcasedir location=${basedir}/test />

<property name=outdir location=${basedir}/classes />

<property name=reportdir location=${basedir}/report />

<property name=junitdir location=${reportdir}/junit />

<property name=junitxmldir location=${junitdir}/xml />

<property name=junithtmldir location=${junitdir}/html />

<property name=coberturadir location=${reportdir}/cobertura />

<property name=coberturaxmldir location=${coberturadir}/xml />

<property name=coberturahtmldir location=${coberturadir}/html />

<property name=instrumenteddir location=${reportdir}/instrumented />

<property name=instrumentfile location=${instrumenteddir}/coberturaser />

<property name=coberturadir value=${instrumenteddir} />

<path id=classpathall>

<pathelement location=${outdir} />

<fileset id=alljars dir=${libdir}>

<include name=**/*jar />

</fileset>

</path>

<!– include the cobertura building jars –>

<path id=coberturaclasspath>

<path refid=classpathall />

</path>

<!– define the cobertura property file –>

<taskdef classpat resource=tasksproperties/>

<!– =================================================================== –>

<!– Project Targets –>

<!– =================================================================== –>

<target name=init description=initialize the project env>

<!– create the output folder –>

<mkdir dir=${outdir} />

<mkdir dir=${reportdir} />

<copy todir=${outdir}>

<fileset dir=${srcdir} includes=**/*properties />

</copy>

</target>

<target name=compile depends=init>

<javac srcdir=${srcdir} destdir=${outdir}>

<classpath refid=classpathall />

</javac>

</target>

<!– =================================================================== –>

<!– Unit Test Targets –>

<!– =================================================================== –>

<!–

target: init

initialize the build env

–>

<target name=inittest description=initialize the test env>

<!– create the output folder –>

<mkdir dir=${junitdir} />

<mkdir dir=${junitxmldir} />

<mkdir dir=${junithtmldir} />

</target>

<!–

target: compiletest

compile the test cases

–>

<target name=compiletest depends=compile>

<javac srcdir=${testcasedir} destdir=${outdir}>

<classpath refid=classpathall />

</javac>

</target>

<!– =================================

target: test

run the unit test

================================= –>

<target name=test depends=inittest>

<junit fork=yes printsummary=on maxmemory=m>

<sysproperty key=berturadatafile

file=${instrumentfile} />

<classpath location=${instrumenteddir} />

<classpath refid=coberturaclasspath />

<formatter type=xml />

<batchtest todir=${junitxmldir}>

<fileset dir=${outdir}>

<include name=**/Test*class />

<exclude name=**/*$*class />

</fileset>

</batchtest>

</junit>

<junitreport todir=${junitxmldir}>

<fileset dir=${junitxmldir}>

<include name=TEST*xml />

</fileset>

<report format=frames todir=${junithtmldir} />

</junitreport>

</target>

<!– =================================================================== –>

<!– Code Coverage Targets –>

<!– =================================================================== –>

<!–

target: init

initialize the build env for code coverage

–>

<target name=initcoverage description=initialize the build env>

<!– create the output folder –>

<mkdir dir=${instrumenteddir} />

<mkdir dir=${coberturadir} />

<mkdir dir=${coberturaxmldir} />

<mkdir dir=${coberturahtmldir} />

</target>

<target name=compilecoverage depends=init>

<javac srcdir=${srcdir}:${testcasedir} destdir=${outdir} debug=true >

<classpath refid=coberturaclasspath />

</javac>

</target>

<!– =================================

target: instrument

Instrument into the class files but

exclude test classes

================================= –>

<target name=instrument depends=initcoveragecompilecoverage description=instrument into the class files>

<!–

Instrument the application classes writing the

instrumented classes into ${instrumenteddir}

–>

<coberturainstrument todir=${instrumenteddir} datafile=${instrumentfile}>

<!–

The following line causes instrument to ignore any

source line containing a reference to logj for the

purposes of coverage reporting

–>

<ignore regex=orgapachelogj* />

<fileset dir=${outdir}>

<!–

Instrument all the application classes but

dont instrument the test classes

–>

<include name=**/*class />

<exclude name=**/Test*class />

</fileset>

</coberturainstrument>

</target>

<!– =================================

target: coveragecheck

check the code coverage by given rates

================================= –>

<target name=coveragecheck description=check the code coverage by given rates>

<coberturacheck branchrate= totallinerate= />

</target>

<!– =================================

target: coveragereportxml

generate code coverage report by xml format

================================= –>

<target name=coveragereportxml description=generate xml report>

<!– Generate an XML file containing the coverage data using the srcdir attribute –>

<coberturareport srcdir=${srcdir} destdir=${coberturaxmldir} format=xml datafile=${instrumentfile}/>

</target>

<!– =================================

target: coveragereporthtml

generate code coverage report by html format

================================= –>

<target name=coveragereporthtml>

<!–

Generate a series of HTML files containing the coverage

data in a userreadable form using nested source filesets

–>

<coberturareport destdir=${coberturahtmldir} datafile=${instrumentfile}>

<fileset dir=${srcdir}>

<include name=**/*java/>

</fileset>

</coberturareport>

</target>

<!– run the code coverage individual –>

<target name=coverage depends=compilecoverageinstrumenttestcoveragereporthtml

description=Compile instrument ourself run the tests and generate JUnit and code coverage reports/>

<!– =================================================================== –>

<!– Public Targets –>

<!– =================================================================== –>

<target name=clean>

<delete quiet=true includeEmptyDirs=true>

<fileset dir=${reportdir}>

<exclude name=cvsignore />

<exclude name=CVS />

</fileset>

<fileset dir=${outdir}>

</fileset>

</delete>

</target>

<!– =================================================================== –>

<!– Global Targets –>

<!– =================================================================== –>

<target name=all depends=compile />

<target name=junit depends=clean compiletest test />

<target name=full depends=clean coverage />

</project>

作者: Cherami

原载: Ant+JUnit+Cobertura

版权所有转载时必须以链接形式注明作者和原始出处及本声明

上一篇:Struts2简介

下一篇:抽取spring数据库连接部分到项目中