Clover在JUnit中的使用

下面还是简要的介绍一下Clover在JUnit中的使用(构建工具为Ant):
    1.在使用Clover做测试覆盖率分析之前请确保您的代码及测试代码无误。
    2.如果采用Ant作为构建工具,则将clover.jar放入Ant的classpath下。也可采用另一种方法,可先将clover.jar及Clover中的另一个jar包cenquatasks.jar放入项目的lib下,采用cenquatasks.jar中的extendclasspath方法将clover.jar扩展到Ant下,在build.xml中加入以下代码:

< taskdef  resource ="com/cenqua/ant/antlib.xml"  classpath ="lib/cenquatasks.jar" />
< extendclasspath  path ="lib/clover.jar" />
< taskdef  resource ="clovertasks"  classpath ="lib/clover.jar" />
    3.添加clovertask到Ant中,如下:
< taskdef  resource ="clovertasks" />
    4.在build.xml中加入clover的初始化任务:
   <!--  clover.init -->
  
< target  name ="clover.init" >
    
< clover-setup  initString ="clover_coverage.db"  preserve ="true" />
  
</ target >
注意:步骤3中的代码可放入4中。
    5.加入一个或多个运行clover report的任务,最后的结果有多种输出方式:html、pdf、swing、xml等等。
其中,swing方式的代码如下:
  <!--  show the clover coverage result with swing  -->
   
< target  name ="clover.swing"  depends ="clover" >
        
< clover-view />
   
</ target >
html方式如下:
  <!--  show the clover coverage result with html file  -->
 
< target  name ="clover.html"  depends ="clover" >
    
< clover-report >
      
< current  outfile ="${target.dir}/clover"  title ="Clover Report" >
        
< format  type ="html" />  
      
</ current >      
    
</ clover-report >
  
</ target >
    上面代码中都depends="clover",这里clover任务只是为了简洁而设,代码如下:
<!--  runs the clover  -->
    
< target  name ="clover"  depends ="clean,clover.init,compile,test" />
    通过以上代码的设置即可运行clover.swing 或者clover.html运行Clover查看测试代码的覆盖率。测试结果如下图所示:
     其中build.xml中的运行JUnit测试代码的任务如下:
  <!--  runs the tests  -->
     
< target  name ="test" >
        
< java  classname ="MoneyTest"   fork ="yes" >
            
< classpath  refid ="build.classpath" />
        
</ java >
     
</ target >
这是通过java命令运行的,也就是在MoneyTest.java中有main方法:
public   static   void  main(String args[])  {
        junit.textui.TestRunner.run(MoneyTest.
class);
    }
还可以直接通过junit命令运行,如下:
< target  name ="unittest"  depends ="clover.init,compile"  description ="Test the application" >
        
< taskdef  resource ="clovertasks" />
        
< echo  message ="Testing the application with junit.." />
        
< junit  haltonfailure ="true"  printsummary ="true"   fork ="true" >
                
< classpath  refid ="build.classpath" />
                
< classpath >
                        
< pathelement  path ="${ant.home}/lib/clover.jar" />
                
</ classpath >
                
< formatter  type ="brief"  usefile ="false" />
    
<!--             <test name="MoneyTest"/>    -->
                
< batchtest >
                        
< fileset  dir ="${build}"  includes ="**/*Test.class" />
                
</ batchtest >
        
</ junit >
</ target >
以上两种test方法都可使用。
    在所有任务中,首先要运行clover.init这样就能启动clover,对后面的任务进行监控并adapt一些操作,在随后的compile过程中,clover会记录所编译的java文件的各种数据并存入其db数据库,clover会监控随后运行的UnitTest,计算测试的覆盖率并写入数据库。这里有一点要注意:无论是采用java方式还是junit方式运行test房间,都要在命令中加入属性fork="true",这是因为clover要在JVM shutdown的时候将数据写入数据库,如果没有这个属性,就会出现“no coverage data found for *.db”错误,出现测试覆盖率均为0的结果。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值