三、修改build.xml文件添加clover任务
现在我们开始修改build.xml文件,添加clover任务,以实现junit测试覆盖率的分析。首先,我们添加clover任务的配置任务;在build.xml文件里添加如下代码:
这两行定义了能够在ant文件里使用clover任务的配置工作。然后再添加一个clover任务,用来启动clover构建任务:
这里的initString定义了clover覆盖数据库的信息,它会在执行clover任务时自动创建,里边保存了源路径执行测试任务时候的全部信息,clover通过这个demo_coverage.db来创建覆盖率分析报告。如果这个demo_coverage.db存在,clover在每次运行时自动覆盖,如果不存在,clover自动创建它。
我们这里使用clover,是为了搜集junit测试时候的覆盖率信息,所以呢,junit测试过程中,我们需要使用clover.jar包,所以我们还需要把clover.jar文件作为junit运行的依赖库,因此修改build.classpath代码成如下所示:
这里顺便我们也把path的值说明一下,省得有人不懂哦!这里的ant.home是在计算机里创建的环境变量ANT_HOME,它的值应该是你的ant的安装路径,例如c:ant,所以前两行代码的意思,就是把c:antlibclover.jar和c:antlibjunit.jar作为依赖库。当然,事先你一定要拷贝这两个jar文件到你的lib目录。
好了,我们现在开始重新使用clover任务来运行junit测试!
C:cloversincky>ant clean
Buildfile: build.xml
clean:
[delete] Deleting directory C:cloversinckybuild
BUILD SUCCESSFUL
Total time: 0 seconds
C:cloversincky>ant clover compile
Buildfile: build.xml
clover:
[clover-setup] Clover Version 1.3.8, built on May 18 2005
[clover-setup] loaded from: C:cloverlibclover.jar
[clover-setup] 30 day Evaluation License registered tosinckyzhang@163.com
[clover-setup] You have 10 day(s) before your Evaluation License expires.
[clover-setup] Clover is enabled with initstring 'C:cloversinckydemo_coverage.db'
compile:
[mkdir] Created dir: C:cloversinckybuild
[javac] Compiling 4 source files to C:cloversinckybuild
[clover] Clover Version 1.3.8, built on May 18 2005
[clover] loaded from: C:cloverlibclover.jar
[clover] 30 day Evaluation License registered tosinckyzhang@163.com
[clover] You have 10 day(s) before your Evaluation License expires.
[clover] No coverage database 'C:cloversinckydemo_coverage.db' fou
nd. Creating a fresh one.
[clover] Clover all over. Instrumented 4 files.
BUILD SUCCESSFUL
Total time: 2 seconds
Buildfile: build.xml
clean:
[delete] Deleting directory C:cloversinckybuild
BUILD SUCCESSFUL
Total time: 0 seconds
C:cloversincky>ant clover compile
Buildfile: build.xml
clover:
[clover-setup] Clover Version 1.3.8, built on May 18 2005
[clover-setup] loaded from: C:cloverlibclover.jar
[clover-setup] 30 day Evaluation License registered tosinckyzhang@163.com
[clover-setup] You have 10 day(s) before your Evaluation License expires.
[clover-setup] Clover is enabled with initstring 'C:cloversinckydemo_coverage.db'
compile:
[mkdir] Created dir: C:cloversinckybuild
[javac] Compiling 4 source files to C:cloversinckybuild
[clover] Clover Version 1.3.8, built on May 18 2005
[clover] loaded from: C:cloverlibclover.jar
[clover] 30 day Evaluation License registered tosinckyzhang@163.com
[clover] You have 10 day(s) before your Evaluation License expires.
[clover] No coverage database 'C:cloversinckydemo_coverage.db' fou
nd. Creating a fresh one.
[clover] Clover all over. Instrumented 4 files.
BUILD SUCCESSFUL
Total time: 2 seconds
我们会发现在sincky目录下多了一个demo_coverage.db文件;之后我们再执行测试任务:
C:cloversincky>ant test
Buildfile: build.xml
test:
[java] ......................
[java] Time: 0
[java] OK (22 tests)
BUILD SUCCESSFUL
Total time: 0 seconds
Buildfile: build.xml
test:
[java] ......................
[java] Time: 0
[java] OK (22 tests)
BUILD SUCCESSFUL
Total time: 0 seconds