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>
相关文章推荐
- Java单元测试Junit(六)使用Cobertura生成测试覆盖率报告
- Ant执行JUnit测试并生成报告
- 结合junit + ant + emma实现Android项目单元测试代码覆盖率统计
- Ant结合Junit生成测试报告
- ant+junit 自动生成测试报告
- 利用ant 和 Junit 生成测试报告
- ant学习笔记(三)ant整合junit做测试并生成测试报告
- Ant/Junit: 利用Ant执行测试用例并生成测试报告
- Ant执行JUnit测试并生成报告
- ant 构建junit 测试生成报告
- Karma +Jasmine+ require JS进行单元测试并生成测试报告、代码覆盖率报告
- [置顶] Android 使用Ant+Emma+Junit收集android代码覆盖率(来自联想的一位同事2012.12.24)
- sonar ant+jacoco 编译测试并出具测试代码覆盖率报告
- ant + junit 测试。并生成测试报告
- ANT调用JMeter执行脚本并生成html测试报告
- Ant中批量调用TestNG的XML文件,并调用TestNgXlst生成漂亮的html测试报告
- Ant环境配置、生成测试报告以及问题的解决
- ARM-linux下的代码覆盖率报告生成
- 用Emma的Eclipse插件进行代码覆盖率测试
- ant+junit执行自动化并且生成report报告