我如何让Maven的Emma或Cobertura报告其他模块中的源代码报道? [英] How do I get Emma or Cobertura, with Maven, to report coverage on source code in other modules?

查看:158
本文介绍了我如何让Maven的Emma或Cobertura报告其他模块中的源代码报道?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有Java代码的多模块Maven设置。

I have a multi-module Maven setup with Java code.

我的单元测试,在其中一个模块中,在多个模块中练习代码。当然,这些模块具有相互依赖关系,所有相关模块中的代码都是在测试执行之前根据需要进行编译的。

My unit tests, in one of the modules, exercise code in multiple modules. Naturally, the modules have inter-dependencies, and code in all relevant modules is compiled as needed in advance of test execution.

所以:我怎样才能得到关于整个代码库的覆盖范围?

So: How can I get a report on the coverage of the entire codebase?

注意:我不是要问如何在多个模块中结合测试的覆盖率结果。我问如何使用来自多个模块的检测代码在单个模块中获得测试的覆盖率。任何对前者感兴趣的人都可以参考这些 其他 问题,以及Crowne针对 Maven Dashboard 声纳

Note: I am not asking how to combine the results of coverage for tests in multiple modules. I am asking how to get coverage for tests in a single module using instrumented code from multiple modules. Anyone interested in the former might refer to these other questions, and the recommendations by Crowne for Maven Dashboard and Sonar.

我成功获得完整报道使用纯蚂蚁。 我将开发运行时目录中的所有jar都检测到了一个临时目录;将临时目录添加到类路径中;然后使用批处理测试从Ant 运行测试

I succeeded in getting a full coverage report using pure Ant. I instrumented all jars from the development-runtime directory into a temporary directory; prepended the temporary directory to the classpath; then ran tests from Ant with batch-test.

Ant可以从Maven运行,但这里的挑战是无缝集成(即自动将所有类路径和源路径元素从Maven提供给Ant),这就是为什么我没有为此目的使用Maven的工具。

Ant can be run from Maven, but the challenge here is seamless integration (i.e., feeding all the classpath and sourcepath elements from Maven to Ant automatically), which is why I did not use Maven's facilities for this purpose.

其他 问题。但是,默认情况下,默认情况下,每个项目的报告仅报告相同项目中代码的覆盖范围,而我的测试则在多个项目中执行代码。

There are also other questions about integration tests. However, by default, each project's report by default only reports coverage on code in the same project, whereas my tests exercise code in multiple projects.

西班牙语的文章可能是相关的。这是另一个 Seam特定的文章

This article in Spanish might be relevant. Here is another Seam-specific article.

推荐答案

Thomas Sundberg最近的博客文章包含一种方法,通过使用ant进行cobertura调用而不是使用maven cobertura插件来部分解决问题。

This recent blog post by Thomas Sundberg contains a method that partially solves the issue by using ant for the cobertura calls, instead of using the maven cobertura plugin.

它依赖于以下基本方法和专门的pom.xml和build.xml文件:

It relies on the following basic approach with specialised pom.xml and build.xml files :

从父pom上的典型maven编译,它将编译子模块中的所有类。

Start with a typical maven compile on the parent pom, which will compile all classes in the child modules.

mvn clean compile # maven-compile-plugin called for compiling

然后检测所有模块类:

ant instrument # cobertura called for instrumentation

然后使用检测类调用maven-surefire-plugin进行测试,使用cobertura作为测试依赖项

Then call the maven-surefire-plugin called for testing using the instrumented classes, with cobertura as a test dependency

mvn test 

然后使用自定义报告调用从不同模块中提取所有结果:

Then use a custom report call to pull in all of the results from different modules:

ant report # cobertura called for reporting

ant build.xml文件的关键元素是分别检测所有模块,然后报告在合并结果后的所有模块上。在他的示例中,需要为每个模块调用此函数:

The key elements of the ant build.xml file are to instrument all modules separately and then to report on all of the modules after merging the results. This function needs to be called for each module in his example:

<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>

然后在测试完成后,报告阶段首先合并来自所有不同目录的所有结果合并到一个新的.ser文件(在他的例子中称为sum.ser)

Then after the testing is complete, the reporting phase first merges all results from all of the different directories are merged into a new .ser file (called sum.ser in his example)

<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>

有可能使用antrun插件将ant组件集成到maven中,但我不熟悉足够的阶段/生命周期知道在哪里放置不同的调用。

It may be possible to integrate the ant components into maven using the antrun plugin, but I am not familiar enough with the phases/lifecycles to know where to put the different calls.

这对我来说非常有用,因为我在我的api模块中编写抽象测试类然后提供他们在我的lib模块中有一个实现。到目前为止,cobertura和emma都无法处理这种设计,因此我的代码覆盖率通常为0或单个数字。

This is very useful for me, as I write abstract test classes in my api modules and then provide them with an implementation in my lib modules. So far both cobertura and emma have been unable to handle this design so my code coverage is typically 0 or in the single digits.

这篇关于我如何让Maven的Emma或Cobertura报告其他模块中的源代码报道?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆