繁体   English   中英

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

[英]Maven multi module project with separate tests module - Code Coverage?

我有一个 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

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

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

有一种方法可以实现这一点。 神奇的是创建一个组合的 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>

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

我不认为jacococobertura能够报告跨模块的代码覆盖率。 您可能想运行测试覆盖率报告之前尝试检测已编译的类而不是依赖即时检测。

请参阅此jacoco maven目标以执行离线检测。

从 Jacoco 版本:0.7.7 开始,您可以使用report-aggregate

根 pom.xml :

<project>
[...]
  <build>
    <plugins>
     <!--   refer:https://prismoskills.appspot.com/lessons/Maven/Chapter_06_-_Jacoco_report_aggregation.jsp     -->
    <plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.6</version>
    <executions>
        <execution>
            <id>prepare-agent</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
        </execution>

        <execution>
            <id>report</id>
            <phase>test</phase>
            <goals>
                <goal>report</goal>
                <goal>report-aggregate</goal>
            </goals>
        </execution>
    </executions>
    </plugin>
    <plugins>
  </build>
[...]
<pluginManagement>
  <plugins>
    <!--    unit test plugin        -->
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>3.0.0-M5</version>
      <dependencies>
        <dependency>
          <groupId>org.apache.maven.surefire</groupId>
          <artifactId>surefire-junit47</artifactId>
          <version>3.0.0-M5</version>
        </dependency>
      </dependencies>
      <configuration>
        <argLine>${argLine} -Dfile.encoding=UTF-8</argLine>
      </configuration>
    </plugin>
  </plugins>
</pluginManagement>
[...]
</project>

子模块 pom.xml:

<project>
[...]
<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
            <includes>
                <include>[path]</include>
            </includes>
        </configuration>
    </plugin>
</plugins>
[...]
</project>

如果你使用 Jenkin,你可以只使用 jacoco 插件和<goal>report</goal>而没有其他新东西。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM