jacoco多模块生成单元测试报告

网上的方案来来回回都那么几篇,博客抄来抄去简直吐血,坐牢一样排查了两天后得出以下方案,实测springcloud多模块可行,利用evosuite生成的单元测试代码+手动编写的单元测试代码已达到行覆盖率85%的要求(这要求属实离谱)。
另外此方案jacoco为on the fly模式,offline模式应该也是可行,参考另一篇文章
关于on the fly 模式与 offline区别查看资料:https://www.jacoco.org
官方示例参考:https://github.com/jacoco/jacoco/tree/master/jacoco-maven-plugin.test/it
话不多说直接上代码

根目录pom.xml增加

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.5</version>
    <executions>
        <execution>
            <id>prepare-agent</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
        </execution>
    </executions>
</plugin>
<!--maven 指定jdk版本 -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <source>${java.version}</source>
        <target>${java.version}</target>
        <encoding>${project.build.sourceEncoding}</encoding>
    </configuration>
</plugin>

新增一个模块,整合所有单元测试文件,依赖所有子模块
---- parent
| ---- service-module 业务代码模块
| ---- test-module 单元测试模块
| ---- application-module 包含启动类模块
test-module 依赖application-module、service-module
所有单元测试代码在test-module的src/test/java下,包括新写的代码

test-module 的pom.xml

<!--把依赖的模块解压出来放到classes目录下,原依赖的模块都是jar包方式存在,mvn test时会找不到-->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>unpack</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>unpack</goal>
            </goals>
            <configuration>
                <artifactItems>
                    <!--把每个依赖的模块,都配置进来-->
                    <artifactItem>
                        <groupId>com.lishiots.cloud.datacenter</groupId>
                        <artifactId>datacenter-service</artifactId>
                        <version>1.0.1</version>
                        <type>jar</type>
                        <includes>**/*.class</includes>
                        <overWrite>false</overWrite>
                        <outputDirectory>${project.build.directory}/classes</outputDirectory>
                    </artifactItem>
                </artifactItems>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <!--  junit 5 版本至少为2.22.2,且需要增加其他依赖 -->
    <version>2.7.1</version>
    <configuration>
        <runOrder>random</runOrder>
        <testFailureIgnore>true</testFailureIgnore>
    </configuration>
</plugin>
<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.5</version>
    <executions>
        <!--整合所有子模块报告-->
        <execution>
            <id>report-aggregate</id>
            <phase>test</phase>
            <goals>
                <goal>report-aggregate</goal>
            </goals>
        </execution>
    </executions>
</plugin>

依次执行命令

先编译

mvn clean -U -Dmaven.test.skip=true package

保证test-module的target/classes中存在依赖的模块代码
如果启动需要依赖yml文件,需要先替换target/classes/bootstart.yml中的占位符

再执行测试

mvn test

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容