设为首页 加入收藏

TOP

maven 多模块项目的测试覆盖率分析 - jacoco 聚合分析(二)
2023-07-25 21:38:56 】 浏览:41
Tags:maven 项目的 jacoco
scope>test</scope> </dependency> </dependencies>

然后在 src/test/java 目录下创建测试类:

// test-module\src\test\java\org\example\IntegrationTest.java
package org.example;

import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class IntegrationTest {

    private IntegerSimpleCompute simpleCompute;
    private IntegerLogicCompute logicCompute;

    @Before
    public void init() {
        simpleCompute = new IntegerSimpleCompute();
        logicCompute = new IntegerLogicCompute();
    }

    @Test
    public void simpleComputeTest() throws Throwable {
        assertEquals(7, simpleCompute.add(3, 4));
        assertEquals(4, simpleCompute.subtract(7, 3));
        assertEquals(12, simpleCompute.multiply(3, 4));
        assertEquals(3, simpleCompute.divide(12, 4));
    }

    @Test
    public void logicComputeTest() throws Throwable {
        assertEquals(8, logicCompute.increment(7));
        assertEquals(6, logicCompute.decrement(7));
        assertEquals(true, logicCompute.equals(125, 125));
        assertEquals(false, logicCompute.equals(123, 125));
        assertEquals(false, logicCompute.equals(123, 130));
        assertEquals(false, logicCompute.equals(133, 125));
        assertEquals(true, logicCompute.equals(140, 140));
        assertEquals(false, logicCompute.equals(140, 141));
    }
}

到可以,你可以通过:

mvn test

执行单元测试,maven 的 maven-surefire-plugin 插件也会简单的输出如下测试报告:

Tests run: 2, Failures: 0, Errors: 0, Skipped: 0

四:生成覆盖率报告

首先在根目录的 pom.xml 引入 jacoco 插件并且启动代理:

<build>
    <plugins>
        <!-- 指定 Java 编译版本 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>11</source>
                <target>11</target>
            </configuration>
        </plugin>

        <!-- jacoco 插件 -->
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.8.8</version>
            <executions>
                <!--  执行 prepare-agent 目标,它会启动 JaCoCo 代理 -->
                <execution>
                    <id>default-prepare-agent</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>

                <!-- 执行 mvn verify 时,生成测试覆盖率报告 -->
                <execution>
                    <id>report</id>
                    <phase>verify</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

然后在 test-module 模块中引入 jacoco 插件,声明一个聚合分析任务:

<build>
    <plugins>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.8.8</version>
            <executions>
                <!--  在执行 mvn verify 时,生成聚合测试覆盖率报告,所有 Maven 子模块的测试覆盖率数据 -->
                <execution>
                    <id>report-aggregate</id>
                    <phase>verify</phase>
                    <goals>
                        <goal>report-aggregate</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

最后在根目录执行指令,运行所有测试:

$ mvn clean verify

构建成功后可以在 test-module 模块下的 target/site/jacoco-aggregate/index.html 查看覆盖率报告:

image-20230309142038873

点击对应模块可以看到包内部所有类,方法还有每一行的测试覆盖率情况

首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇项目中多级缓存设计实践总结 下一篇Apache Dubbo 官方正式发布 Sprin..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目