当前位置: 首页 > 知识库问答 >
问题:

如何使用Jacoco在多maven模块上强制执行单元测试覆盖

卢英范
2023-03-14

我有下面的多maven模块应用程序,我遵循了下面的步骤https://github.com/jacoco/jacoco/wiki/MavenMultiModule在测试模块中生成一份综合的jacoco单元测试覆盖率报告。然而,我们有一个政策来执行80%的最低总覆盖率,否则构建失败。这在单模块应用中非常有效。然而,我无法在多模块应用程序的总体覆盖范围内实施这一点。如有任何建议,将不胜感激。

Parent-
-Module1
-Module2
-test

我试图为测试模块的pom文件设置jacoco check-in的执行目标,但clean install成功了,没有任何覆盖错误。

家长pom.xml

......

    <modules>
        <module>module1</module>
        <module>module2</module>
        <module>tests</module>

    </modules>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath /> 
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
        <maven-surefire-plugin.version>3.0.0-M1</maven-surefire-plugin.version>
        <jacoco.version>0.8.3</jacoco.version>



    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>



    <build>
        <plugins>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.7.9</version>
                <executions>
                    <execution>
                        <id>prepare-unit-tests</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>prepare-agent</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                        <phase>pre-integration-test</phase>
                        <configuration>
                            <propertyName>itCoverageAgent</propertyName>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>`

测试聚甲醛。xml

`

<modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.myapp.parent</groupId>
    <artifactId>myservice</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>myapp-tests</artifactId>
  <properties>
        <code.coverage.project.folder>${basedir}/../</code.coverage.project.folder>
        <code.coverage.overall.data.folder>${basedir}/../target/aggregate.exec</code.coverage.overall.data.folder>
        <code-coverage.line-covered-ratio.min>0.84</code-coverage.line-covered-ratio.min>
  </properties>

    <dependencies>
        <dependency>
           <groupId>module1</groupId>
           <artifactId>myapp-module1</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
           <groupId>module1</groupId>
           <artifactId>myapp-module2</artifactId>
            <version>1.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <!-- Jacoco prepare-agent builds some command-line params without -->
                    <!-- which jacoco will not instrument. Hence it is important to add -->
                    <!-- those command-line params here (${argLine} holds those params) -->
                    <argLine>${argLine} -Xms256m -Xmx2048m</argLine>
                    <forkCount>1</forkCount>
                    <runOrder>random</runOrder>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <executions>
                <execution>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>report</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>jacoco-check</id>
                        <goals>
                            <goal>check</goal>
                        </goals>
                        <configuration>
                            <rules>
                                <rule>
                                    <element>PACKAGE</element>
                                    <limits>
                                        <limit>
                                            <counter>LINE</counter>
                                            <value>COVEREDRATIO</value>
                                            <minimum>${code-coverage.line-covered-ratio.min}</minimum>
                                        </limit>
                                    </limits>
                                </rule>
                            </rules>
                        </configuration>
                    </execution>
                    <execution>
                        <id>report-aggregate</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>report-aggregate</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>merge-results</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>merge</goal>
                        </goals>
                        <configuration>
                            <fileSets>
                                <fileSet>
                                    <directory>${code.coverage.project.folder}</directory>
                                    <includes>
                                        <include>**/target/jacoco.exec</include>
                                    </includes>
                                </fileSet>
                            </fileSets>
                            <destFile>${code.coverage.overall.data.folder}/aggregate.exec</destFile>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

`

如果总覆盖率不是80%,我预计mvn clean install会抛出错误

共有1个答案

杨柏
2023-03-14

您只是在测试模块上强制覆盖。

您应该移动此配置:

  <execution>
    <id>jacoco-check</id>
    <goals>
        <goal>check</goal>
    </goals>
    <configuration>
        <rules>
            <rule>
                <element>PACKAGE</element>
                <limits>
                    <limit>
                        <counter>LINE</counter>
                        <value>COVEREDRATIO</value>
                        <minimum>${code-coverage.line-covered-ratio.min}</minimum>
                    </limit>
                </limits>
            </rule>
        </rules>
    </configuration>
  </execution>

进入您的父pom。这样所有模块都将被检查。

 类似资料:
  • 我有一个基本的maven多模块,父模块是POM,其子模块是domain、service和Web。 我已经在pom父级中添加了jacoco插件和所需的配置,以便将测试覆盖率报告附加到一个Sonar很容易找到的文件中。 我的问题是,Sonar只显示第一个模块的测试覆盖范围(是domain)(即使我用编辑器打开文件,看到其他模块的类名被追加在其中)。 这种配置会有什么问题? 为了它的价值,声纳分析被称为

  • null client.admin(eclipse-plugin打包) client.admin.test.fragment(eclipse-test-plugin packaging) sonar.junit.reportsPath sonar.jacoco.reportpath, sonar.jacoco.itreportpath sonar.core.codecoveragePlugin s

  • 问题内容: 似乎有几个问题,这些问题已经很老了,并且从Java 8对Jacoco的支持开始发生了变化。 我的项目包含以下结构 我已经配置了这样的 主POM.xml Pom.xml B pom.xml 我正在执行此命令。我可以看到jacoco.exec正在生成,但是我看不到任何HTML报告正在验证数据。 请帮我解决一下这个。还有一点是,我的配置对项目是否正确? 更新资料 已确定的问题。 变成 现在,

  • 似乎有几个问题,这些问题很老了,而且从Java 8对Jacoco的支持开始就发生了变化。 我的项目包含以下结构 我已经这样配置了 主要聚甲醛.xml 一个Pom.xml B pom.xml 我正在执行这个命令< code>mvn clean package。我可以看到jacoco.exec正在生成,但是我看不到任何验证数据的HTML报告。 请帮帮我。另一点是,我的配置对于项目是否正确? 更新 已识

  • 在使用maven运行单元测试时,我遇到了这个异常。我的所有测试都没有执行。我的测试类的格式是 我正在运行以下命令来运行此命令: 使用的surefire插件是: 有人知道为什么我的测试没有执行吗?我用的是jUnit 4.8.2和surefire 2.14.1

  • 我正试图在詹金斯的sonarqube仪表板上获取代码覆盖率报告。代码覆盖率报告即将发布,但仅显示4.6%的覆盖率。经过调查,我发现使用PowerMock编写的测试类被跳过了。 经过进一步调查,我发现“JaCoCo不能很好地处理动态修改/创建的类(这就是powermock的工作方式)。这是一个已知的限制,我们目前对此无能为力”。 是否有任何解决办法,以便我也可以为使用PowerMock编写的测试类获