带有单独测试模块的Maven多模块项目-代码覆盖率? [英] Maven multi module project with separate tests module - Code Coverage?
问题描述
我有一个Maven多模块项目.
I have a maven multi module project.
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/的单个模块中,所有代码均在单独的模块中.
All tests are in single module called tests/ and all code is in separate modules.
有没有办法获得代码覆盖率?
Is there a way I can get code coverage?
推荐答案
有一种方法可以实现此目的.魔术是创建一个组合的jacoco.exec文件,并分两步进行.我的pom:
There is a way to accomplish this. The magic is to create a combined jacoco.exec file and to do it in two steps. My 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 -DrunTestWithJacoco
,然后执行mvn validate -DcreateJacocoReport
.现在,您已经全面了解了一个类,而哪个测试覆盖了它都无所谓.魔术是使用maven.multiModuleProjectDirectory
创建组合的jacoco.exec文件.从maven 3.3.1开始,此属性可用,它指向您开始进行maven构建的文件夹.
Add this to your parent pom and execute mvn clean install -DrunTestWithJacoco
and than mvn validate -DcreateJacocoReport
. Now you have the complete coverage of a class and it doesn't matter which test covered it. The magic is to use maven.multiModuleProjectDirectory
to create a combined jacoco.exec file. This property is available since maven 3.3.1 and points to the folder where you started your maven build.
这篇关于带有单独测试模块的Maven多模块项目-代码覆盖率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!