【问题标题】:Maven multi module project with separate tests module - Code Coverage?具有单独测试模块的 Maven 多模块项目 - 代码覆盖率?
【发布时间】:2015-01-10 13:43:21
【问题描述】:

我有一个 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/ 的模块中,所有代码都在单独的模块中。

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

【问题讨论】:

  • 单元测试根据定义是适当模块的本地测试,而不是提取到单独的模块。因此,您对单独的单元测试模块的设置没有意义。 Maven 的目的是在同一个模块中拥有生产代码和适当的单元测试。由于文件夹结构显示src/main/java 生产代码。 src/test/java 单元测试代码。
  • 如果您将测试模块视为集成测试的位置,我认为这是一个有效的设置。目前看来 jacoco 不支持这个,如果你有心情真正做到这一点,你可以试试 Sonar。否则,请等待 Jacoco 的下一个版本,它可能实际上支持它:D (github.com/jacoco/jacoco/pull/97)
  • 如果我们谈论的是集成测试,是的,但问题中没有写。
  • 如果你喜欢用 jacoco 运行集成测试,你需要一个代理,它必须在 maven-failsafe-plugin(如果我们谈论集成测试)中适当地配置。见这里:petrikainulainen.net/programming/maven/…

标签: java maven code-coverage jacoco-maven-plugin


【解决方案1】:

有一种方法可以做到这一点。神奇的是创建一个组合的 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 的文件夹。

【讨论】:

    【解决方案2】:

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

    查看此jacoco maven goal 以执行离线检测。

    【讨论】:

    • 在 jacoco 中,您不需要检测,因为它有一个可以相应配置的代理。
    【解决方案3】:

    由于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>
    

    如果你使用 Jenkins,你可以只使用 jacoco 插件和&lt;goal&gt;report&lt;/goal&gt; 没有其他新东西。

    【讨论】:

      猜你喜欢
      • 2014-10-13
      • 2020-10-29
      • 2017-06-24
      • 2014-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-12
      • 1970-01-01
      相关资源
      最近更新 更多