[英]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.