简体   繁体   English

如何使用 jacoco 在多 Maven 模块上强制执行单元测试覆盖率

[英]How to enforce unit test coverage on multi maven module with jacoco

I have following multi maven module application and I followed the steps from here https://github.com/jacoco/jacoco/wiki/MavenMultiModule to generate a consolidated jacoco unit test coverage report in tests module.我有以下多 Maven 模块应用程序,我按照https://github.com/jacoco/jacoco/wiki/MavenMultiModule 中的步骤在测试模块中生成合并的 jacoco 单元测试覆盖率报告。 However, we have a policy to enforce the 80% of minimum overall coverage otherwise build fails.但是,我们有一项政策强制执行最低总覆盖率的 80%,否则构建将失败。 This works perfectly in single module application.这在单模块应用程序中完美运行。 However, I am not able to enforce this on overall coverage of multi module application.但是,我无法在多模块应用程序的整体覆盖范围内强制执行此操作。 Any suggestions would be appreciated.任何建议,将不胜感激。

Parent-
-Module1
-Module2
-test

I tried to set set up an execution goal of jacoco check in pom file of the tests modules but clean install succeeds without any coverage error.我试图在测试模块的 pom 文件中设置 jacoco 检查的执行目标,但干净安装成功而没有任何覆盖错误。

Parent pom.xml `父 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>`

test pom.xml测试 pom.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>

` `

I expect mvn clean install to throw error if the aggregate coverage is not 80%如果总覆盖率不是 80%,我希望 mvn clean install 会抛出错误

You are only enforcing the coverage on test module.您只是在测试模块上强制执行覆盖率。

You should move this configuration:您应该移动此配置:

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

Into your parent pom.进入你的父 pom。 This way all modules will be checked.这样所有模块都将被检查。

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

相关问题 多模块Maven:测试覆盖率Jacoco声纳 - Multi-module maven : test coverage jacoco sonar SonarQube Jacoco离线仪器在Maven多模块项目中显示0%的覆盖率 - SonarQube Jacoco offline instrumentation shows 0% coverage in Maven Multi Module Project 如何使用 jacoco 检查多模块 maven 项目的最小代码覆盖率? - How to check minimum code coverage for multi-module maven project with jacoco? 如何配置多模块 Maven + Sonar + JaCoCo 以提供合并的覆盖率报告? - How to configure multi-module Maven + Sonar + JaCoCo to give merged coverage report? Jacoco、Maven 和 TestNG(0 测试覆盖率) - Jacoco, Maven & TestNG (0 test coverage) JaCoCo与多模块Maven项目-仅记录模块拥有的测试的覆盖率 - JaCoCo with multi-module Maven project - Only record coverage by module owned tests 使用jacoco和maven集成测试代码覆盖率 - Integration Test Code Coverage using jacoco and maven JaCoCo 测试覆盖率问题 maven 构建失败 - JaCoCo test coverage issue maven build failing 使用 Maven 在 Azure DevOps 中构建多模块项目的 Jacoco 代码覆盖问题 - Issue with Jacoco code coverage for a multi-module project build in Azure DevOps using Maven Maven / Jacoco-将黄瓜测试结果添加到Jacoco代码覆盖率概述 - Maven/Jacoco - add Cucumber test results to the Jacoco code coverage overview
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM