具有独立测试模块的Maven多模块项目-代码覆盖率?

nis*_*ant 5 java code-coverage maven jacoco-maven-plugin

我有一个Maven多模块项目。

root:
    moduleA/   # no unit tests
    moduleB/   # no unit tests
    moduleC/   # no unit tests
    tests/     # All unit tests, since depends on modules A, B and C
Run Code Online (Sandbox Code Playgroud)

所有测试都在称为tests /的单个模块中,所有代码都在单独的模块中。

有没有办法获得代码覆盖率?

Sve*_*ann 5

有一种方法可以实现这一点。神奇的是创建一个组合的 jacoco.exec 文件并分两步完成。我的pom:

<properties>
    ...
    <jacoco.overall.exec>${maven.multiModuleProjectDirectory}/target/jacoco_analysis/jacoco.exec</jacoco.overall.exec>
</properties>

<build>
    <pluginManagement>
        <plugins>

            ...

            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.7.8</version>
                <configuration>
                    <destFile>${jacoco.overall.exec}</destFile>
                    <dataFile>${jacoco.overall.exec}</dataFile>
                </configuration>
            </plugin>

            ...

        </plugins>
    </pluginManagement>
</build>

<profiles>
    <profile>
        <id>runTestWithJacoco</id>
        <activation>
            <property>
                <name>runTestWithJacoco</name>
                <value>true</value>
            </property>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.jacoco</groupId>
                    <artifactId>jacoco-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>default-prepare-agent</id>
                            <goals>
                                <goal>prepare-agent</goal>
                            </goals>
                            <configuration>
                                <append>true</append>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
    <profile>
        <id>createJacocoReport</id>
        <activation>
            <property>
                <name>createJacocoReport</name>
                <value>true</value>
            </property>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.jacoco</groupId>
                    <artifactId>jacoco-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>default-report</id>
                            <phase>validate</phase>
                            <goals>
                                <goal>report</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>
Run Code Online (Sandbox Code Playgroud)

将此添加到您的父 pom 并执行mvn clean install -DrunTestWithJacocoand than mvn validate -DcreateJacocoReport。现在您已经完全覆盖了一个类,并且哪个测试覆盖它都无关紧要。神奇的是用来maven.multiModuleProjectDirectory创建一个组合的 jacoco.exec 文件。此属性自 Maven 3.3.1 起可用,并指向您开始构建 Maven 的文件夹。


mys*_*cks 0

我认为两者都没有jacoco能力cobertura报告跨模块的代码覆盖率。您可能希望在运行测试覆盖率报告之前尝试对已编译的类进行检测,而不是依赖于即时检测。

请参阅此jacocoMaven目标来执行离线检测。