Cobertura 是一款优秀的开源测试覆盖率统计工具( http://cobertura.sourceforge.net ), Maven 通过 cobertura-maven-plugin 与之集成,可以使用 mvn cobertura:cobertura 命令生成单元测试覆盖率报告。报告会在项目的 target/site/bobertura 目录下生成 html 文件,点击 index.html 文件可以看到报告详情。
一 maven 插件
参考官方文档: http://mojo.codehaus.org/cobertura-maven-plugin/plugin-info.html
官方提供的 maven 插件,大致有以下 goal :
--------------------------------------------------------------------------------
Goal Description
cobertura:check Check the Last Instrumentation Results.
cobertura:clean Clean up rogue files that cobertura maven plugin is tracking.
cobertura:dump-datafile Cobertura Datafile Dump Mojo
cobertura:instrument Instrument the compiled classes.
cobertura:cobertura Instruments, Tests, and Generates a Cobertura Report.
--------------------------------------------------------------------------------
Maven 官方提供的 Cobertura 插件只支持标准的测试发布路径(有些喜欢将测试类和工程类编译到同一个路径下,但是 Maven 官方推荐的路径确实将测试类和工程类分开编译到不同的路径下),那首先需要修改编译路径,如果在你的 Project POM 中没有对编译路径、资源路径做过任何声明,那 Maven 就会默认取标准路径,如果你的工程真的没有设置这些信息,那么,你很幸运,可以跳过此步。但是如果你的路径不是标准路径,请按以下方式进行修改:
xml 代码
1. < sourceDirectory > src/main/java</ sourceDirectory >
2. < scriptSourceDirectory > src/main/scripts</ scriptSourceDirectory >
3. < testSourceDirectory > src/test/java</ testSourceDirectory >
4. < outputDirectory > target/classes</ outputDirectory >
5. < testOutputDirectory > target/test-classes</ testOutputDirectory >
通常情况下我们只需要进行如下配置就足可以应付当前的测试需求了:
xml 代码
1. < reporting >
2. < outputDirectory > target/site</ outputDirectory >
3. < plugins >
4. < plugin >
5. < groupId > org.codehaus.mojo</ groupId >
6. < artifactId > cobertura-maven-plugin</ artifactId >
7. </ plugin >
8. </ plugins >
9. </ reporting >
当 QA 拿到 test case 运行的 coverage reporting 时,通常希望能够进行打包以和开发人员取得联系,本人目前尚未找到合适的 Maven 打包方法,由于在 Maven2 中支持执行 Ant 脚本,因此我在这里为 build 节点中的 plugins 节点提供了一个用 ant 脚本写的 plugin ,在 Maven 命令行通过执行 site 命令便可以将最后的测试报告(包括测试代码覆盖率和测试用例的运行情况 reports )进行打包
xml 代码
1. < plugin >
2. < artifactId > maven-antrun-plugin</ artifactId >
3. < executions >
4. < execution >
5. < phase > site</ phase >
6. < configuration >
7. < tasks >
8. < copy todir ="target/classes" >
9. < fileset dir ="src/main/java" >
10. < include name ="**/*.java" />
11. </ fileset >
12. </ copy >
13. < jar destfile ="target/cover-test-result.zip" >
14. < fileset dir ="target/site" >
15. < include name ="**/*.html" />
16. < include name ="**/*.css" />
17. < include name ="**/*.txt" />
18. < include name ="**/*.png" />
19. < include name ="**/*.js" />
20. </ fileset >
21. </ jar >
22. </ tasks >
23. </ configuration >
24. < goals >
25. < goal > run</ goal >
26. </ goals >
27. </ execution >
28. </ executions >
29. </ plugin >
继续说 Cobertura 的附加配置:
通过上面的配置,我们已经可以对一个工程进行代码覆盖率测试了,但是 Cobertura 在你没有声明 test case 不用分析覆盖率的情况下,他会连同 test case 一起分析覆盖率,尽管这样不会影响我们整个工程的代码覆盖率,但是在生成的报告中多出一个 testcase 来,总让人感觉不是很舒服,因此,你可以采用 Cobertura 的一些附加配置来限制那些不用进行覆盖率测试的类,具体做法如下:
xml 代码
1. < plugin >
2. < groupId > org.codehaus.mojo</ groupId >
3. < artifactId > cobertura-maven-plugin</ artifactId >
4. < configuration >
5. < instrumentation >
6. <!--<ignore>.*</ignore>
7. -->
8. < excludes >
9. < exclude > **/*Test.class</ exclude >
10. </ excludes >
11. </ instrumentation >
12. </ configuration >
13. < executions >
14. < execution >
15. < goals >
16. < goal > clean</ goal >
17. </ goals >
18. </ execution >
19. </ executions >
20. </ plugin >
二 命令行模式
参考官方文档: http://cobertura.sourceforge.net/commandlinereference.html
1
插桩
cobertura-instrument.sh [ CODE_SOURCE_DIR | *.jar] --datafile=*.ser 会直接替换原有的 class 文件
2 合并
cobertura-merge.sh --datafile cobertura.ser 1.ser 2.ser…
合并到第一个文件中
3
产生报告
cobertura-report.sh --datafile *.ser --destination DIR --basedir SOURCE_CODE
生成报告文件到某个目录,默认为html
--format xml :
报告生成xml 文件
-Dnet.sourceforge.cobertura.datafile=${basedir}/cobertura.ser
指定生成的覆盖率文件生成的路径