如何使用Jacoco在多Maven模块上实施单元测试覆盖率

时间:2019-07-14 12:35:25

标签: maven spring-boot jacoco-maven-plugin

我已经关注了多Maven模块的应用程序,并且按照此处https://github.com/jacoco/jacoco/wiki/MavenMultiModule的步骤进行操作,以在测试模块中生成合并的jacoco单元测试覆盖率报告。但是,我们有一项政策强制执行最低总覆盖率的80%,否则构建会失败。这在单个模块应用程序中完美运行。但是,我无法在多模块应用程序的总体覆盖范围上强制执行此操作。任何建议,将不胜感激。

Parent-
-Module1
-Module2
-test

我试图在测试模块的pom文件中设置jacoco check的执行目标,但干净安装成功,没有任何覆盖错误。

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

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

`

如果总覆盖率不是80%,我希望mvn全新安装会引发错误

1 个答案:

答案 0 :(得分:1)

您仅在 test 模块上强制覆盖。

您应该移动此配置:

  <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。 这样,将检查所有模块。