jacoco仅显示同一模块中类的覆盖率 [英] jacoco only shows coverage for classes in the same module

查看:312
本文介绍了jacoco仅显示同一模块中类的覆盖率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个比较大的多模块Maven项目.我在Jacoco处理的每个模块中都有单元测试.我有一个单独的子模块在执行合并"和报告聚合",这似乎正在生成数据.我什至在SonarQube中使用生成的数据.我的大多数测试都使用PowerMock,而我使用的是脱机检测.

I have a somewhat large multi-module Maven project. I have the unit tests in each module being processed by Jacoco. I have a separate child module doing "merge" and "report-aggregate", and this appears to be generating data. I'm even using the generated data in SonarQube. Most of my tests are using PowerMock, and I'm using offline instrumentation.

但是,仔细查看覆盖率数据后,我发现它遗漏了我知道在测试期间正在执行的类和方法的覆盖率数据.我在每个模块中看到的模式是,它仅报告每个模块中单个类的覆盖率,而该类实际上是当前模块中的一个类.几乎所有测试都还调用构建中其他模块中的其他类,并且从未报告这些类的覆盖率.

However, after looking closer at the coverage data, I see that it is leaving out coverage data for classes and methods that I know are being executed during tests. The pattern I see in every module is that it only reports coverage for a single class in each module, which is a class actually in the current module. Almost all of the tests also call out to other classes in other modules in the build, and coverage for those classes are never reported.

以下插件配置位于每个子模块使用的父pom中:

The following plugin configurations are in the parent pom used by each child module:

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.7.8</version>
    <executions>
        <execution>
            <id>default-instrument</id>
            <goals>
                <goal>instrument</goal>
            </goals>
        </execution>
        <execution>
            <id>default-restore-instrumented-classes</id>
            <goals>
                <goal>restore-instrumented-classes</goal>
            </goals>
        </execution>
        <execution>
            <id>default-report</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>report</goal>
            </goals>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.19.1</version>
    <configuration>
        <argLine>-Xmx1024m</argLine>
        <includes>
            <include>**/*Test.java</include>
        </includes>
        <systemPropertyVariables>
            <jacoco-agent.destfile>${project.build.directory}/jacoco.exec</jacoco-agent.destfile>
            <running-unit-test>true</running-unit-test>
        </systemPropertyVariables>
    </configuration>
</plugin>

当我检查每个模块生成的HTML结果时,我发现它仅报告当前模块中单个类的结果,而不报告其他模块中类的数据.由此,我认为我如何在单独的子模块中合并"和报告汇总"可能与该问题无关.

When I inspect the generated HTML results for each module, I find that it only reports results for the single class in the current module, and not the data for classes in other modules. From this, I would assume that how I do "merge" and "report-aggregate" in the separate child module is probably irrelevant to this problem.

生成的"jacoco.exec"文件是二进制文件,但是我尝试从一个模块中捕获"一个文件以查看可见的ascii文本,并且该文件仅显示一次类似于文件名的内容,并且这是该模块的HTML覆盖率报告中报告的唯一文件名.

The generated "jacoco.exec" file is binary, but I tried "catting" out one from one module just to see what ascii text was visible, and it showed only one occurrence of anything that looked like a file name, and it was the only file name reported in the HTML coverage report for that module.

我不确定我还可以报告什么其他信息.

I'm not sure what other information I can report.

更新:

我想我现在可以很清楚地看到,当surefire运行单元测试时,它将使用当前模块中的检测类,而使用maven工件中的未检测类.这就是为什么我只看到当前模块中类的覆盖范围的原因.

I guess I can see pretty clearly now that when surefire runs unit tests, it uses the instrumented classes from the current module, but the uninstrumented classes from the maven artifacts. This is why I only see coverage for classes in the current module.

因此,似乎我需要一种方法来指定当前模块所依赖的每个模块的"target/generate-classes/jacoco"文件夹被添加到surefire使用的类路径之前.我没有办法做到这一点.

So it seems like I need a way to specify that the "target/generated-classes/jacoco" folder for each module the current module depends on, is prepended to the classpath that surefire uses. I don't see a way to do that.

或者,我看到工具"目标有一个包含"配置元素.我应该为当前模块所依赖的每个模块指定所有目标/类"目录的路径吗?

Alternatively, I see that the "instrument" goal has an "includes" configuration element. Should I be specifying paths to all of the "target/classes" directories for each of the modules that the current module depends on?

推荐答案

记录某些类的代码覆盖率需要对其进行检测.目标instrument执行当前模块类的检测.

Recording of code coverage for some class requires its instrumentation. Goal instrument performs instrumentation of classes of current module.

所有测试还调用其他模块中的其他类

all of the tests also call out to other classes in other modules

所以那些没有检测的.如果我正确理解的话,那么正是您所缺少的内容.

so the ones that are not instrumented. And if I correctly understood, then exactly those for which you are missing coverage.

如果您不对来自其他模块的类使用PowerMock,而仅对当前模块中的类使用PowerMock,则可以将脱机检测与使用代理进行实时组合.但是在这种情况下,请确保由代理明确将脱机检测的类从检测中明确排除,否则代理将抛出IllegalStateException: Class ... is already instrumented.

If you don't use PowerMock for classes that come from other modules, but only for classes in current module, then you can combine offline instrumentation with on-the-fly using agent. But in this case make sure that classes instrumented offline are explicitly excluded from instrumentation by agent, otherwise agent will be throwing IllegalStateException: Class ... is already instrumented.

如果将PowerMock用于来自其他模块的类,则由于Maven在使用类路径和依赖项进行操作方面的严格性,这将变得更加复杂.而且我怀疑是否可以使用一个mvn指令轻松实现这一点,但是似乎可以使用更多的

If you use PowerMock for classes that come from other modules, then this becomes more complex due to strictness of Maven in regards of manipulations with classpaths and dependencies. And I doubt that this can be easily achieved using one mvn comand, however seems possible using more:

  1. 仪器并运行测试,但不要使用restore-instrumented-classes
  2. 还原类并生成报告

很遗憾,您没有提供完整的示例( https://stackoverflow.com/help/mcve ),但是我没有提供现在没有时间准备完整的示例来测试这种方法.

Unfortunately you haven't provided complete example (https://stackoverflow.com/help/mcve) and I don't have time to prepare full example to test this approach right now.

请注意:PowerMock无法绕过任何代理程序并从磁盘读取类文件,因此无法简单地使用代理程序.

As a side note: inability to simply use agent comes from the fact that PowerMock bypasses any agent and reads class files from disk.

这篇关于jacoco仅显示同一模块中类的覆盖率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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