您的位置:首页 > 编程语言

junit + ant + emma生成代码测试覆盖率报告

2012-05-29 09:50 696 查看

源程序:
public class SampleCalculator

{
public
int add(int
augend , int addend)

{
return augend + addend;

}

public
int subtration(int
minuend , int subtrahend)

{
return minuend - subtrahend;

}
}


测试程序
public class SampleCalculatorTest
extends TestCase
{
public
void testAdd()// thhrows IOException
{
SampleCalculator calculator = new SampleCalculator();
int result = calculator.add(50,20);
assertEquals(70,result);
}
public
void testSubtration()
{
SampleCalculator calculator = new SampleCalculator();
int result = calculator.subtration(50,20);
assertEquals(30,result);
}
}



下面便是测试的ant脚本了,注意修改对应的目录位置和记得引入emma类库时,需要放到对应目录。还有就是需要安装junit和ant。

junit通过assert捕捉错误。



<?xml version="1.0"?>




<project name="SampleProject"
basedir="."
default="all">


<!--You may notice that, I use location instead of value -->


<!--Both of them work correctly when I run ant -f build.xml -->


<!--But when this file is called by other ant file using ant task-->


<!--In the junit task, the relative path cannot be parsed correctly-->


<!--When using location, the path will be replaced with absolute path-->


<!--The libs contains the emma.jar and emma_ant.jar-->


<property
name="libs"
location="../lib"
/>


<!--This is where we place our instrumented classes-->


<property
name="bin.instrument.dir"
location="../instrbin"
/>


<!--coverage metadata and report location-->


<property
name="coverage.dir"
location="../coverage"
/>


<!--junit report location-->


<property
name="junitReport.dir"
location="../junitReport"
/>


<!--main bin location-->


<property
name="bin.main.dir"
location="../srcbin"
/>


<!--test bin location-->


<property
name="bin.test.dir"
location="../testbin"
/>


<!--main source location-->


<property
name="src.main.dir"
location="../../SampleProject/src"
/>


<!--test source location-->


<property
name="src.test.dir"
location="../../SampleProjectTest/test"
/>


<!--Instrument classes in this path-->


<path
id="classpath.main">


<pathelement
location="${bin.main.dir}"
/>


</path>


<!--Path where emma.jar and emma_ant.jar exist-->


<path
id="emma.lib">


<pathelement
location="${libs}/emma.jar"
/>


<pathelement
location="${libs}/emma_ant.jar"
/>


</path>


<!--enable emma-->


<property
name="emma.enabled"
value="true"
/>


<!--Add emma task to ant-->


<taskdef
resource="emma_ant.properties"
classpathref="emma.lib"
/>


<!--Default task-->


<target
name="all"
depends="clean,compile-src.main,compile-src.test,
instrument,test,gen-report-junit,gen-report-coverage">


</target>


<target
name="compile-src.main">


<mkdir
dir="${bin.main.dir}"
/>


<javac
destdir="${bin.main.dir}"
includeantruntime="on"


debug="on">


<src
path="${src.main.dir}"
/>


</javac>


<copy
todir="${bin.main.dir}">


<fileset
dir="${src.main.dir}">


<exclude
name="**/*.java"
/>


</fileset>


</copy>


</target>


<target
name="compile-src.test">


<mkdir
dir="${bin.test.dir}"
/>


<javac
destdir="${bin.test.dir}"
includeantruntime="on"
debug="on">


<src
path="${src.test.dir}"
/>


<classpath
location="${bin.main.dir}"
/>


</javac>


<copy
todir="${bin.test.dir}">


<fileset
dir="${src.test.dir}">


<exclude
name="**/*.java"
/>


</fileset>


</copy>


</target>


<!--Instrument the src main bin, place the instrumented class in bin.instrument.dir-->


<!--metadata file will be placed in coverage.dir-->


<target
name="instrument">


<mkdir
dir="${bin.instrument.dir}"
/>


<mkdir
dir="${coverage.dir}"
/>


<emma
enabled="${emma.enabled}">


<instr
instrpathref="classpath.main"
destdir="${bin.instrument.dir}"
metadatafile="${coverage.dir}/metadata.emma"
merge="true">


</instr>


</emma>


<copy
todir="${bin.instrument.dir}">


<fileset
dir="${bin.main.dir}">


<exclude
name="**/*.java"
/>


</fileset>


</copy>


</target>


 
 
<!--Run test case, generate junit report and coverage report-->


<target
name="test">


<mkdir
dir="${junitReport.dir}"
/>


<junit
fork="true"
forkmode="once"
printsummary="withOutAndErr"
errorproperty="test.error"
showoutput="on">


 
<!--Refer to metadata.emma to collect run information-->


<jvmarg
value="-Demma.coverage.out.file=${coverage.dir}/metadata.emma"
/>


<jvmarg
value="-Demma.coverage.out.merge=true"
/>


<!--Test support package-->


<classpath
location="${bin.instrument.dir}"
/>


<classpath
location="${bin.test.dir}"
/>


<classpath
refid="emma.lib"
/>


<formatter
type="xml"
/>


<!--Batch test exclude inner class-->


<batchtest
todir="${junitReport.dir}"
haltonfailure="no">


<fileset
dir="${bin.test.dir}">


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


</fileset>


</batchtest>


</junit>


</target>


<target
name="gen-report-junit">


<!--Generate junit report-->


<junitreport
todir="${junitReport.dir}">


<fileset
dir="${junitReport.dir}">


<include
name="*"
/>


</fileset>


<report
format="frames"
todir="${junitReport.dir}"
/>


</junitreport>


</target>


<!--Generate the coverage report-->


<target
name="gen-report-coverage">


<!-- if enabled, generate coverage report(s): -->


<emma
enabled="${emma.enabled}">


<report
sourcepath="${src.main.dir}"
sort="+block,+name,+method,+class"
metrics="method:70,block:80,line:80,class:100">


<fileset
dir="${coverage.dir}">


<include
name="*.emma"
/>


</fileset>


<html
outfile="${coverage.dir}/coverage.html"
depth="method"
columns="name,class,method,block,line"
/>


</report>


</emma>


</target>


<!--Clean srcbin, instrumented bin,junit report, coverage report-->


<target
name="clean">


<delete
dir="${bin.instrument.dir}"
/>


<delete
dir="${coverage.dir}"
/>


<delete
dir="${junitReport.dir}"
/>


<delete
dir="${bin.main.dir}"
/>


<delete
dir="${bin.test.dir}"
/>


</target>




</project>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: