有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

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

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

有没有办法让我得到代码覆盖率


共 (3) 个答案

  1. # 1 楼答案

    我认为jacococobertura都不能跨模块报告代码覆盖率。在运行测试覆盖率报告之前,您可能希望尝试检测编译的类,而不是依赖于动态检测

    请参阅此jacocomaven goal以执行脱机检测

  2. # 2 楼答案

    有一种方法可以做到这一点。魔术是创造一个组合的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 -DrunTestWithJacocomvn validate -DcreateJacocoReport。现在你已经完全覆盖了一个类,不管哪个测试覆盖了它。神奇的是使用maven.multiModuleProjectDirectory创建一个组合jacoco。exec文件。此属性自maven 3.3.1起可用,并指向启动maven构建的文件夹

  3. # 3 楼答案

    由于Jacoco版本:0.7.7,您可以使用report-aggregate

    根聚甲醛。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>,而不需要其他新东西