[First attempt] Introduce jacoco-maven-plugin in Idea Maven project to generate test code coverage report

pom.xmlfile, where <groupId>org.yjm</groupId>to change

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.yjm</groupId>
    <artifactId>JUnit</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <properties>
        <maven.compiler.source>16</maven.compiler.source>
        <maven.compiler.target>16</maven.compiler.target>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.9</version>
                <configuration>
                    <!--定义输出的文件夹-->
                    <outputDirectory>target/jacoco-report</outputDirectory>
                    <!--执行数据的文件-->
                    <dataFile>${project.build.directory}/jacoco.exec</dataFile>
                    <!--要从报告中排除的类文件列表,支持通配符(*和?)。如果未指定则不会排除任何内容-->
                    <!-- <excludes>**/test/*.class</excludes> -->
                    <!--包含生成报告的文件列表,支持通配符(*和?)。如果未指定则包含所有内容-->
                    <includes></includes>
                    <!--HTML 报告页面中使用的页脚文本。-->
                    <footer></footer>
                    <!--生成报告的文件类型,HTML(默认)、XML、CSV-->
                    <formats>HTML</formats>
                    <!--生成报告的编码格式,默认UTF-8-->
                    <outputEncoding>UTF-8</outputEncoding>
                    <!--抑制执行的标签-->
                    <skip></skip>
                    <!--源文件编码-->
                    <sourceEncoding>UTF-8</sourceEncoding>
                    <!--HTML报告的标题-->
                    <title>${project.name}</title>
                </configuration>
                <executions>
                    <execution>
                        <id>jacoco-check</id>
                        <goals>
                            <goal>check</goal>
                        </goals>
                        <configuration>
                            <rules>
                                <rule>
                                    <element>PACKAGE</element>
                                    <limits>
                                        <limit>
                                            <counter>LINE</counter>
                                            <value>COVEREDRATIO</value>
                                            <!-- 配置最低覆盖率为0.1(10%) -->
                                            <minimum>0.1</minimum>
                                        </limit>
                                    </limits>
                                </rule>
                            </rules>
                        </configuration>
                    </execution>
                    <execution>
                        <id>generate-code-coverage-report</id>
                        <phase>test</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

After importing, open the console, take the project communication directory as the path, and run
mvn clean test org.jacoco:jacoco-maven-plugin:0.8.9:prepare-agent install

insert image description here

The generated report can be found in the target, and you can view it by opening index.html

Guess you like

Origin blog.csdn.net/qq_33843237/article/details/130276811