JaCoCo多模块单元测试配置——mavn项目配置

JaCoCo多模块单元测试配置

在maven的多模块项目中配置Jacoco插件,显示多个模块的单元测试覆盖率报告

1.首先创建一个多模块项目

目录结构如下

image

JacocoMoudlesTest是root模块,test-a,test-b,test-c,三个模块中包含单元测试的代码,test-common是个空模块,主要存放全部进行单元测试模块的总覆盖率报告。

以上图为例,common模块主要存放a,b,c模块的单元测试覆盖率报告

2.父模块的pom配置

导入junit和jacoco的依赖

<dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <scope>test</scope>
</dependency>
<dependency>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.8.3</version>
</dependency>

配置jacoco的plugin插件

<plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.3</version>
                <configuration>
                    <!--<includes>-->
                    <!--<include>com/test/demo/*</include>-->
                    <!--</includes>-->
                </configuration>
                <executions>
                    <execution>
                        <id>jacoco-initialize</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>check</id>
                        <goals>
                            <goal>check</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>jacoco-site</id>
                        <phase>package</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                        <configuration>
                            <dataFile>target/jacoco.exec</dataFile>
                            <outputDirectory>target/jacoco-site</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

3.在父模块下运行mvn package

在父模块中配置了goal为report,运行父模块下的mvn package之后会在所有子模块中生成单测覆盖率报告

4.在common模块中显示覆盖率报告总合

在common模块的依赖中添加需要进行单元测试的模块

 <dependency>
        <groupId>com.example</groupId>
        <artifactId>test-a</artifactId>
        <version>0.0.1-SNAPSHOT</version>
 </dependency>
 <dependency>
        <groupId>com.example</groupId>
        <artifactId>test-b</artifactId>
        <version>0.0.1-SNAPSHOT</version>
 </dependency>
 <dependency>
        <groupId>com.example</groupId>
        <artifactId>test-c</artifactId>
        <version>0.0.1-SNAPSHOT</version>
 </dependency>

配置common模块下的jacoco插件,

<build>
    <plugins>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.8.3</version>
            <executions>
                <execution>
                    <id>report-aggregate</id>
                    <phase>test</phase>
                    <goals>
                        <goal>report-aggregate</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

goal为 report-aggregate ,这个goal是jacoco 0.7.7版本以后,专门为多模块覆盖率显示所设置,可以统计该模块所依赖的所有其他模块的覆盖率

image

common下的单测覆盖率报告如图

image

参考博客:https://www.cnblogs.com/tuzhenxian/p/11228261.html

posted @ 2021-09-29 11:21  TidalCoast  阅读(1886)  评论(0编辑  收藏  举报