• 『Maven + Junit + Jacoco』单元测试覆盖率


    示例代码 - 码云

    pom中添加Junit依赖

    <!-- Junit -->
    <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency>

    测试类

    package com.glen.he;
    
    public class ComplexCalculation {
        public int Division(int a, int b) {
            return (a / b);
        }
    
        public int Multiply(int a, int b) {
            return (a * b);
        }
    }
    package com.glen.he;
    import static org.junit.Assert.assertEquals;
    import org.junit.Test;
    
    public class ComplexCalculationTest {
        ComplexCalculation cc = new ComplexCalculation();
    
        @Test
        public void DivisionTest() {
            int c = cc.Division(100, 5);
            assertEquals(20, c);
        }
    
        @Test
        public void MultiplyTest() {
            int c = cc.Multiply(100, 5);
            assertEquals(500, c);
        }
    }

     (先配置idea中terminal  maven的环境变量)执行mvn test

    Maven Surefire Plugin+Junit测试

    在默认情况下,执行maven test/maven package/maven install命令时会在target/surefire-reports目录下生成txt和xml格式的输出信息

    其实maven也可以生成html格式的报告,只需要用一个插件即可:maven-surefire-report-plugin

    pom.xml中添加插件

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>2.5</version>
      <configuration>
        //跳过测试
        <skipTests>true</skipTests>

        //排除测试
        <excludes>
          <exclude>**/*ServiceTest.java</exclude>
        </excludes>

        //执行测试以 Test.java 或 TestCase.java 结尾的文件
        <includes>       <include>**/*Test.java</include>
          <include>**/*TestCase.java</include>     </includes>
      </configuration> </plugin>

       1.先编译源文件和测试用例

       2.再调用surefire插件(这个插件主要是用来执行单元测试的插件)生成txt和xml个数的测试输出信息

       3.surefire报告插件(也就是maven-surefire-report-plugin)会把target/surefire-reports下的所有xml报告转换成html格式的文件。并将这个html格式的测试报告保存在target/site目录下

    Jacoco

    pom.xml添加插件

    <plugin>
       <groupId>org.jacoco</groupId>
       <artifactId>jacoco-maven-plugin</artifactId>
       <version>0.8.3</version>
       <configuration>
           <skip>false</skip>
       </configuration>
    
      <executions>
          <execution>
             <goals>
                <goal>prepare-agent</goal>
             </goals>
       </execution>
    
       <execution>
           <!-- 指定报告输出目录 -->
           <configuration>
              <outputDirectory>${basedir}/target/coverage-reports</outputDirectory>
           </configuration>
           <id>report</id>
           <phase>test</phase>
              <goals>
                 <goal>report</goal>
              </goals>
        </execution>
    
     </executions>
    </plugin>

    直接启动下图1会生成测试报告.html 和 覆盖率报告.html

     target下的jacoco.exec文件也可以通过下图中的方式查看覆盖率报告

     

     

    说明:绿色  覆盖

         红色  未执行覆盖

          

    Maven动态指令测试用例

      mvn test -Dtest=类名         //执行单个测试类

      mvn test -Dtest=类名,类名  //逗号或* 分割执行多个测试类

    命令:mvn test surefire-report:report -Dmaven.test.skip=true  执行生成html文件

       mvn cobertura:cobertura  生成覆盖率报告

  • 相关阅读:
    DM8168 新三板系统启动
    AndroidAndroid程序提示和消息button响应事件
    Linux核心regulator建筑和准备
    对话(VC_Win32)
    VOJ 1067 Warcraft III 守望者的烦恼 (矩阵高速功率+dp)
    有所述网络流的上限和下限
    Matlab Newton‘s method
    Cocos2d-Java安装和配置跨平台游戏引擎以及相关的开发工具
    n每个计数的概率和发生骰子--动态规划
    自己写CPU第九阶段(3)——加载存储指令说明2(swl、swr)
  • 原文地址:https://www.cnblogs.com/yan-sh/p/13932161.html
Copyright © 2020-2023  润新知