• 【Maven】单元测试、统计、覆盖率相关插件使用介绍


    maven-surefire-plugin

    • maven-surefire-pluginmaven执行单元测试的插件,不显性配置也可以直接使用。
    • 这个插件的surefire:test命令会默认绑定maven执行的test阶段。
    • 执行结束后,默认在target/surefire-reports目录下会生成txtxml两种格式的结果,不利于直观展示,需要结合其它插件一起使用。

    如果你自己声明了,那么可以指定自己的版本,并且可以配置自定义的参数。

    配置示例

    <plugin>
        <groupId>org.apache.maven.pluginsgroupId>
        <artifactId>maven-surefire-pluginartifactId>
        <configuration>
            
            <skipTests>falseskipTests>
            
            <testFailureIgnore>truetestFailureIgnore>
            <argLine>${argLine}argLine>
        configuration>
    plugin>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • argLine参数说明
    • jacoco:report-aggregate聚合报告后,覆盖率显示为0,与这个参数有关
    • jacoco在prepare-agent阶段会生成一个属性指向jacoco的runtime agent,默认这个属性叫argLine
    • 需要在maven-surefire-plugin的配置中,引用这个属性

    maven-surefire-report-plugin

    单元测试生成html报告,默认位置target/site/surefire-report.html

    配置示例

    <plugin>
        <groupId>org.apache.maven.pluginsgroupId>
        <artifactId>maven-surefire-report-pluginartifactId>
        <version>${maven-surefire-report-plugin.version}version>
        <executions>
            <execution>
                <id>reportid>
                <phase>testphase>
                <goals>
                    <goal>reportgoal>
                goals>
            execution>
        executions>
    plugin>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    执行mvn test时,会生成报告

    效果预览

    在这里插入图片描述

    jacoco-maven-plugin

    用于生成代码覆盖率报告,可以帮助我们了解代码中哪些部分已经被测试覆盖,哪些部分需要更多的测试,默认位置target/site/jacoco/index.html

    配置示例

    <plugin>
        <groupId>org.jacocogroupId>
        <artifactId>jacoco-maven-pluginartifactId>
        <executions>
            <execution>
                <goals>
                    <goal>prepare-agentgoal>
                goals>
                <configuration>
                    
                    <propertyName>argLinepropertyName>
                configuration>
            execution>
            <execution>
                <id>reportid>
                <phase>testphase>
                <goals>
                    <goal>reportgoal>
                goals>
            execution>
        executions>
        <configuration>
            
            <append>falseappend>
            
            <rules>
                <rule implementation="org.jacoco.maven.RuleConfiguration">
                    <element>BUNDLEelement>
                    <limits>
                        
                        <limit implementation="org.jacoco.report.check.Limit">
                            <counter>METHODcounter>
                            <value>COVEREDRATIOvalue>
                            <minimum>0.80minimum>
                        limit>
                        
                        <limit implementation="org.jacoco.report.check.Limit">
                            <counter>INSTRUCTIONcounter>
                            <value>COVEREDRATIOvalue>
                            <minimum>0.80minimum>
                        limit>
                        
                        <limit implementation="org.jacoco.report.check.Limit">
                            <counter>LINEcounter>
                            <value>COVEREDRATIOvalue>
                            <minimum>0.80minimum>
                        limit>
                        
                        <limit implementation="org.jacoco.report.check.Limit">
                            <counter>CLASScounter>
                            <value>MISSEDCOUNTvalue>
                            <maximum>0maximum>
                        limit>
                    limits>
                rule>
            rules>
        configuration>
    plugin>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58

    效果展示

    在这里插入图片描述

    如果覆盖率显示为0,需要修改maven-surefire-plugin插件的配置

    在这里插入图片描述

  • 相关阅读:
    66 内网安全-域横向批量at&schtasks&impacket
    努力前行,平凡的我们也能画出一条星光闪耀的轨迹——人大女王金融硕士项目
    云平台功能:应用回收站的诞生与使用说明
    clickhouse读取kafka数据
    「学习笔记」重修左偏树
    Android系统源码目录详解
    Web 开发相关概念
    servlet API
    Git小书系列笔记
    [Java] 异常的使用
  • 原文地址:https://blog.csdn.net/friendlytkyj/article/details/130900505