【问题标题】:Can JaCoCo instrument groovy scripts?JaCoCo 可以使用 groovy 脚本吗?
【发布时间】:2021-08-30 14:20:46
【问题描述】:

我正在尝试使用 JaCoCo 检测 groovy 脚本,但似乎 JaCoCo 只能检测 groovy 类。我可以在报告中看到脚本,但它总是以 0% 的覆盖率显示它们。 我正在使用 GroovyShell 执行测试中的脚本。似乎因为脚本的执行方式,JaCoCo 无法将源代码与测试匹配。

这是我的 JaCoCo 配置:

dependency>
    <groupId>org.jacoco</groupId>
    <artifactId>org.jacoco.agent</artifactId>
    <classifier>runtime</classifier>
    <scope>test</scope>
    <version>0.8.7</version>
</dependency>

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.7</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>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>
    <configuration>
        <systemPropertyVariables>
            <jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
        </systemPropertyVariables>
    </configuration>
</plugin>

【问题讨论】:

    标签: groovy jacoco jacoco-maven-plugin groovyshell


    【解决方案1】:

    JaCoCo 可以检测 groovy 脚本。

    没有必要使用离线检测。

    JaCoCo 无法匹配文件,因为我正在编译脚本,但是当脚本执行时,GroovyShell 再次编译它们,生成具有不同类 ID 的其他字节码。 JaCoCo 使用类 ID 来匹配执行的类和分析的类。 解决方法是让 JaCoCo 代理转储目标文件夹下处理的文件,然后我们将拥有相同的类 ID,一切都会正常工作。

    使用这种方法,JaCoCo 也会为所有库生成报告。为避免这种情况,我们需要指定要检测的包。

    我们还需要从 gmavenplus-plugin 中移除 compile 目标,否则脚本会被编译两次,JaCoCo 会抛出重复类名的错误。

    这是我最终的 pom 配置:

    <plugin>
        <groupId>org.codehaus.gmavenplus</groupId>
        <artifactId>gmavenplus-plugin</artifactId>
        <version>1.12.1</version>
        <executions>
            <execution>
                <goals>
                    <goal>addSources</goal>
                    <goal>addTestSources</goal>
                    <!-- <goal>compile</goal>-->
                    <goal>compileTests</goal>
                </goals>
            </execution>
        </executions>
        ...
    </plugin>
    
    
    <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.8.7</version>
        <executions>
            <execution>
                <goals>
                    <goal>prepare-agent</goal>
                </goals>
                <configuration>
                    <classDumpDir>target/classes</classDumpDir>
                    <includes>
                        <include>packageToInclude/**</include>
                    </includes>
                </configuration>
            </execution>
            <execution>
                <id>report</id>
                <phase>prepare-package</phase>
                <goals>
                    <goal>report</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    

    进一步阅读类 ID:https://www.eclemma.org/jacoco/trunk/doc/classids.html 关于转储类文件的进一步阅读:https://www.jacoco.org/jacoco/trunk/doc/agent.html

    【讨论】:

      猜你喜欢
      • 2013-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多