Clover 的使用方法解读

来源:互联网 发布:适合nodejs的cms 编辑:程序博客网 时间:2024/05/17 21:49
http://docs.atlassian.com/maven-clover2-plugin/2.5.1/
https://confluence.atlassian.com/display/CLOVER/Clover-for-Maven+2+and+3+Installation+Guide

Clover:
当测试代码执行时,代码覆盖系统会收集哪些语句已被执行的信息,然后将这些信息作为基本的报告;除了这些基本的报告机制,覆盖方法时 还会以多种不同的方式来收集覆盖信息。多种形式的覆盖超越了基本的语句覆盖,包括有条件的覆盖范围,方法入口和路径覆盖。

测试覆盖的类型:
1. 语句覆盖:语句覆盖衡量的是每个语句是否被执行。
2. 分支覆盖:分支覆盖(有时也被称为判定覆盖),衡量的是在流程控制结构中可能的分支。Clover 记录在执行过程中控制结构中的布尔表达式真假。
3. 方法覆盖:方法覆盖衡量的是 一个方法是否被执行。

Clover中覆盖率的计算方法:TPC(Total Coverage Percentage)
TPC = (BT + BF + SC + MC)/(2*B + S + M)
 
where
 
BT - branches that evaluated to "true" at least once
BF - branches that evaluated to "false" at least once
SC - statements covered
MC - methods entered
 
B - total number of branches
S - total number of statements
M - total number of methods

注意:
①在 hudson 或者 Jenkins 上使用的时候是需要要求 job 的类型是 自由风格的;
②在运行 
mvn clover2:setup test clover2:aggregate clover2:clover

命令之前,如果有cd操作,那么,cd在那个目录下 clover就只会运行那个目录下的工程,所以如果我们对子目录做了操作,那么在运行clover的命令前 一定要 cd 到 工程的根目录下。

在maven2或maven3中使用Clover:
插件虽然开源,但是需要在Apache license下运行。 
运行Clover最简单的方法:
mvn clover2:setup test clover2:aggregate clover2:clover

插件使用解读:
1,创建 clover 数据库:用来记录测试代码执行时的日志文件
使用命令 mvn clover2:instrument 
或者在POM的插件配置中配置:
<executions> <execution> <goals> <goal>instrument</goal> </goals> </execution> </executions>

2. 当clover库中有增量数据时:
使用 mvn  clover2:setup 

3. 检查测试覆盖率的百分比:
使用命令 mvn clover2:check -DtargetPercentage=50%
或者:
<configuration>          <targetPercentage>50%</targetPercentage></configuration>
它是建立在clover数据库已被创建的基础之上的。
因此一般调用命令为:
mvn clover2:instrument clover2:check -DtargetPercentage=50%

4. 配置过滤内容:[被过滤的内容不会被统计到代码覆盖率数据当中]
在POM中配置:
     <configuration>          <contextFilters>try,static</contextFilters>      </configuration>
表示 try和静态块中的内容不会背统计到代码覆盖率当中。

5. 要想生成Clover report
必须配置 mvn clover2:clover
或者在POM的插件配置中配置:
<reporting>    <plugins>      [...]      <plugin>        <groupId>com.atlassian.maven.plugins</groupId>        <artifactId>maven-clover2-plugin</artifactId>        <configuration>          [...]        </configuration>      </plugin>    </plugins>  </reporting>

它也是建立在clover数据库已被创建的基础之上的。
因此一般调用命令为:
mvn clover2:instrument clover2:clover

6. 详细指定报告格式:
插件默认生成HTML格式的报告文件,如果你想生成一个PDF或者XML类型的报告,配置具体报告格式即可。
 <configuration>      <generateHtml>false</generateHtml>      <generatePdf>true</generatePdf>      <generateXml>true</generateXml>  </configuration>

7. 生成历史报告:
使用  clover2:save-history 
或在<configuration>中添加:
          <generateHistorical>true</generateHistorical>

8.合计报告结果:
你可以把子模块中的覆盖率数据合计到一个总的覆盖率文件当中;
命令:mvn  clover2:instrument  clover2:aggregate
或在外部POM中配置:
<goals>     <goal>instrument</goal>    <goal>aggregate</goal> </goals>

9.其它:

配置JDK版本


There are three basic parts executed when recording code coverage with Clover.
1. The clover2:setup goal will instrument your Java source files.
2. The test phase is Maven 2 and 3's standard command for running a unit test phase.
3. The clover2:clover goal generates an HTML, XML, PDF or JSON report.

 The command clover2:aggregate goal is used for merging coverage data generated by multi-module projects.