Jos*_*Fox 12 java junit emma cobertura maven
我有一个带有Java代码的多模块Maven设置.
我的单元测试,在其中一个模块中,在多个模块中练习代码.当然,模块具有相互依赖性,并且在测试执行之前根据需要编译所有相关模块中的代码.
那么:我如何获得整个代码库的覆盖率报告?
注意:我不是问如何在多个模块中组合测试的覆盖率结果.我问如何使用来自多个模块的检测代码在单个模块中获得测试的覆盖率.任何对前者感兴趣的人都可以参考这些 其他 问题,以及Crowne对Maven Dashboard和Sonar的建议.
我成功地使用纯Ant获得了完整的报道 .[编辑:]我将开发运行时目录中的所有jar都检测到了一个临时目录; 将临时目录添加到类路径中; 然后使用批处理测试从Ant运行测试.
Ant可以从Maven运行,但这里的挑战是无缝集成(即,将所有类路径和源路径元素从Maven自动提供给Ant),这就是为什么我没有为此目的使用Maven的工具.
关于集成测试还有其他 问题.但是,默认情况下,默认情况下,每个项目的报告仅报告同一项目中代码的覆盖率,而我的测试则在多个项目中执行代码.
该文章中的西班牙可能是相关的.这是另一篇特定于Seam的文章.
这最近的一篇博客由托马斯·桑德博格包含利用蚂蚁的呼叫的Cobertura,而不是使用maven的插件的Cobertura部分解决这一问题的方法.
它依赖于以下基本方法和专门的pom.xml和build.xml文件:
从父pom上的典型maven编译开始,它将编译子模块中的所有类.
mvn clean compile # maven-compile-plugin called for compiling
Run Code Online (Sandbox Code Playgroud)
然后检测所有模块类:
ant instrument # cobertura called for instrumentation
Run Code Online (Sandbox Code Playgroud)
然后调用maven-surefire-plugin调用使用检测类进行测试,将cobertura作为测试依赖项
mvn test
Run Code Online (Sandbox Code Playgroud)
然后使用自定义报告调用从不同模块中提取所有结果:
ant report # cobertura called for reporting
Run Code Online (Sandbox Code Playgroud)
ant build.xml文件的关键元素是分别检测所有模块,然后在合并结果后报告所有模块.需要为他的示例中的每个模块调用此函数:
<target name="instrumentAModule">
<property name="classes.dir" value="target/classes"/>
<cobertura-instrument todir="./${module}/${classes.dir}">
<fileset dir="./${module}/target/classes">
<include name="**/*.class"/>
</fileset>
</cobertura-instrument>
</target>
Run Code Online (Sandbox Code Playgroud)
然后在测试完成后,报告阶段首先将来自所有不同目录的所有结果合并到一个新的.ser文件中(在他的示例中称为sum.ser)
<target name="report" depends="merge">
<property name="src.dir" value="src/main/java/"/>
<cobertura-report datafile="sum.ser"
format="html"
destdir="./target/report">
<!-- Add all modules that should be included below -->
<!-- fileset dir="./MODULE_NAME_TO_REPLACE/${src.dir}"/ -->
<fileset dir="./product/${src.dir}"/>
</cobertura-report>
</target>
<target name="merge">
<cobertura-merge datafile="sum.ser">
<fileset dir=".">
<include name="**/cobertura.ser"/>
</fileset>
</cobertura-merge>
</target>
Run Code Online (Sandbox Code Playgroud)
有可能使用antrun插件将ant组件集成到maven中,但是我不熟悉阶段/生命周期来知道将不同的调用放在哪里.
这对我来说非常有用,因为我在我的api模块中编写抽象测试类,然后在我的lib模块中为它们提供实现.到目前为止,cobertura和emma都无法处理这种设计,因此我的代码覆盖范围通常为0或单个数字.