zoukankan      html  css  js  c++  java
  • ANT task之Junit、JunitReport

    一、ANT任务之Junit:

      学习ANT其实主要是学习ANT的task,ANT众多task中有一个Testing Tasks,它下面有两个任务:Junit和JunitReport,主要用来进行单元测试及生成单元测试报告。

    Testing Tasks  
    Task NameDescription
    Junit

    Runs tests from the Junit testing framework. This task has been tested with JUnit 3.0 up to JUnit 3.7; it won't work with versions prior to JUnit 3.0.

    JunitReport

    Merges the individual XML files generated by the Junit task and applies a stylesheet on the resulting merged document to provide a browsable report of the testcases results.

     官方网址:http://ant.apache.org/manual/index.html  

    <junit>下面可以包含其它元素,例如:

      1、<test>:运行单个TestCase

      2、<batchtest>:运行多个TestCase

      3、<formatter>:定义测试结果输出格式

     还有很多,详细可以参考官方文档。

    二、项目实例:

    由于ant安装比较得简单,网上一搜一大把且现在ecplise基本都带ant,所以本文并未说明如何搭建ant环境。

    另外,在eclipse中可以通过:window->show view 来调出Ant视图

    1、目录结构如下:

    2、SimpleCalculation类代码如下:

    1 package com.glen.he;
    2 
    3 public class SimpleCalculation {
    4     public int Add(int a,int b){        
    5         return (a+b);        
    6     }
    7 
    8 }
    SimpleCalculation

    3、测试类SimpleCalculationTest代码如下:

     1 package com.glen.he;
     2 
     3 import com.glen.he.SimpleCalculation;
     4 
     5 import static org.junit.Assert.*;
     6 import org.junit.Test;
     7 
     8 public class SimpleCalculationTest {
     9 
    10     SimpleCalculation sc = new SimpleCalculation();
    11     
    12     @Test
    13     public void AddTest() {
    14         
    15         int c = sc.Add(3, 5);    
    16         
    17         assertEquals(8, c);        
    18     }
    19 }
    SimpleCalculationTest

    4、在项目要目录下添加build.xml(执行一个测试)文件,内容如下:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <project name="AntDemo" default="junit" basedir=".">
     3     <!-- =================================================================== -->
     4     <!-- 变量设置  -->
     5     <!-- =================================================================== -->
     6 
     7     <!-- 源代码src路径 -->
     8     <property name="src.path" value="src/java"/>
     9     <!-- 编译文件class路径 -->
    10     <property name="build.path" value="build"/>
    11     <!-- 单元测试代码路径 -->
    12     <property name="test.path" value="src/test"/>
    13     <!-- lib包路径 -->
    14     <property name="lib.path" value="lib"/>         
    15         
    16     <!-- =================================================================== -->
    17     <!-- 设置classpath -->
    18     <!-- =================================================================== -->
    19     <path id="compile.path">        
    20         <fileset dir="${lib.path}">
    21             <include name="**/*.jar"/>
    22         </fileset>
    23         
    24         <pathelement path="${build.path}"/>
    25     </path>     
    26 
    27     <!-- =================================================================== -->
    28     <!-- 清除历史编译class -->
    29     <!-- =================================================================== -->
    30     <target name="clean" description="clean">
    31         <delete dir="${build.path}"/>
    32     </target>
    33 
    34     <!-- =================================================================== -->
    35     <!-- 编译测试文件,初始化目录 -->
    36     <!-- =================================================================== -->
    37     <target name="compile" description="compile">
    38         <mkdir dir="${build.path}"/>                        
    39         <javac srcdir="${src.path}" destdir="${build.path}"  classpathref="compile.path"/>
    40         <javac srcdir="${test.path}" destdir="${build.path}"  classpathref="compile.path"/>
    41     </target>      
    42          
    43     <!-- =================================================================== -->
    44     <!-- 执行测试案例 -->
    45     <!-- =================================================================== -->
    46     <target name="junit" depends="clean,compile">
    47         <junit printsummary="true">
    48              <classpath refid="compile.path"/>                  
    49                       
    50              <test name="com.glen.he.SimpleCalculationTest"/>
    51          </junit>
    52      </target>
    53 
    54 </project>
    View Code

    说明: 

    <junit printsummary="true">
      <classpath refid="compile.path"/>
      <test name="com.glen.he.SimpleCalculationTest"/>
    </junit>

    <path id="compile.path">
      <fileset dir="${lib.path}">
        <include name="**/*.jar"/>
      </fileset>
      <pathelement path="${build.path}"/>
    </path

    我们在<junit〉任务下,使用了编译后的.class文件的目录,还有编译所需的jar包所在的目录。 因为,JUnit任务实际就是为我们运行Test类,而不仅仅是像我们发布Ant文件那样只是javac编译,只需要编译所需的Jar包。我们还需要像java任务那样运.class文件,所以必须包括编译后的.class文件。

    5、然后把build.xml文件拖到Ant视图中,如下图,双击junit执行即可。

    6、执行结果:

     1 Buildfile: D:AntTestuild.xml
     2 clean:
     3    [delete] Deleting directory D:AntTestuild
     4 compile:
     5     [mkdir] Created dir: D:AntTestuild  
     6     [javac] Compiling 1 source file to D:AntTestuild
     7    
     8     [javac] Compiling 1 source file to D:AntTestuild
     9 junit:
    10     [junit] Running com.glen.he.SimpleCalculationTest
    11     [junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0.016 sec
    12 BUILD SUCCESSFUL
    13 Total time: 1 second

    三、增强版build.xml

      通过上面第二步,基本可以达到使用ant和junit来进行单元测试,但还远远不够,比如需要批量运行案例,生成报告等,下面会介绍这些内容

    1、使用formatter属性输出junit信息:

    • 修改build.xml文件,增加第16,49,51,57,58,59行代码
    • 修改build.xml文件,修改53行代码,增加了todir属性,指定xml的输出路径。
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <project name="AntDemo" default="junit" basedir=".">
     3     <!-- =================================================================== -->
     4     <!-- 变量设置  -->
     5     <!-- =================================================================== -->
     6 
     7     <!-- 源代码src路径 -->
     8     <property name="src.path" value="src/java"/>
     9     <!-- 编译文件class路径 -->
    10     <property name="build.path" value="build"/>
    11     <!-- 单元测试代码路径 -->
    12     <property name="test.path" value="src/test"/>
    13     <!-- lib包路径 -->
    14     <property name="lib.path" value="lib"/>
    15     <!-- 生成报告junit4.xml路径 -->
    16     <property name="report.path" value="report"/>
    17         
    18     <!-- =================================================================== -->
    19     <!-- 设置classpath -->
    20     <!-- =================================================================== -->
    21     <path id="compile.path">        
    22         <fileset dir="${lib.path}">
    23             <include name="**/*.jar"/>
    24         </fileset>
    25         
    26         <pathelement path="${build.path}"/>
    27     </path>     
    28 
    29     <!-- =================================================================== -->
    30     <!-- 清除历史编译class -->
    31     <!-- =================================================================== -->
    32     <target name="clean" description="clean">
    33         <delete dir="${build.path}"/>
    34     </target>
    35 
    36     <!-- =================================================================== -->
    37     <!-- 编译测试文件,初始化目录 -->
    38     <!-- =================================================================== -->
    39     <target name="compile" description="compile">
    40         <mkdir dir="${build.path}"/>                        
    41         <javac srcdir="${src.path}" destdir="${build.path}"  classpathref="compile.path"/>
    42         <javac srcdir="${test.path}" destdir="${build.path}"  classpathref="compile.path"/>
    43     </target>      
    44          
    45     <!-- =================================================================== -->
    46     <!-- 执行测试案例 -->
    47     <!-- =================================================================== -->
    48     <target name="junit" depends="clean,compile">
    49         <mkdir dir="${report.path}"/>        
    50         <junit printsummary="true" fork="true">        
    51              <formatter type="xml" usefile="true"/>            
    52              <classpath refid="compile.path"/>                                  
    53              <test name="com.glen.he.SimpleCalculationTest" todir="${report.path}" fork="true"/>
    54          </junit>
    55      </target>
    56 
    57     <target name="delete">
    58         <delete dir="${report.path}"/>
    59     </target>
    60     
    61 </project>

    执行junit的task后,在项目report目录下生成了一个名为TEST-com.glen.he.SimpleCalculationTest.xml的xml文件。

    另外:

    <formatter type="xml" usefile="true"/>中type属性值还有plain、brief

    这时会输出一个文本文件,提供测试失败时的详细内容以及每个测试的运行统计。

     2、批量运行单元测试案例:

    •  修改build.xml文件,把步骤7中的第53行代码替换成下面的58~62行代码;
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <project name="AntDemo" default="junit" basedir=".">
     3     <!-- =================================================================== -->
     4     <!-- 变量设置  -->
     5     <!-- =================================================================== -->
     6 
     7     <!-- 源代码src路径 -->
     8     <property name="src.path" value="src/java"/>
     9     <!-- 编译文件class路径 -->
    10     <property name="build.path" value="build"/>
    11     <!-- 单元测试代码路径 -->
    12     <property name="test.path" value="src/test"/>
    13     <!-- lib包路径 -->
    14     <property name="lib.path" value="lib"/>
    15     <!-- 生成报告junit4.xml路径 -->
    16     <property name="report.path" value="report"/>
    17         
    18     <!-- =================================================================== -->
    19     <!-- 设置classpath -->
    20     <!-- =================================================================== -->
    21     <path id="compile.path">        
    22         <fileset dir="${lib.path}">
    23             <include name="**/*.jar"/>
    24         </fileset>
    25         
    26         <pathelement path="${build.path}"/>
    27     </path>     
    28 
    29     <target name="init">
    30         <mkdir dir="${build.path}"/>
    31         <mkdir dir="${report.path}"/>
    32     </target>
    33     
    34     <!-- =================================================================== -->
    35     <!-- 清除历史编译class -->
    36     <!-- =================================================================== -->
    37     <target name="clean" description="clean">
    38         <delete dir="${build.path}"/>
    39     </target>
    40 
    41     <!-- =================================================================== -->
    42     <!-- 编译测试文件,初始化目录 -->
    43     <!-- =================================================================== -->
    44     <target name="compile" depends="init" description="compile">
    45         <javac srcdir="${src.path}" destdir="${build.path}"  classpathref="compile.path"/>
    46         <javac srcdir="${test.path}" destdir="${build.path}"  classpathref="compile.path"/>
    47     </target>      
    48          
    49     <!-- =================================================================== -->
    50     <!-- 执行测试案例 -->
    51     <!-- =================================================================== -->
    52     <target name="junit" depends="compile">                
    53         <junit printsummary="true" fork="true">        
    54              <formatter type="xml" usefile="true"/>        
    55             
    56              <classpath refid="compile.path"/>        
    57             
    58             <batchtest fork="on" todir="${report.path}" haltonfailure="no">
    59                 <fileset dir="${build.path}">
    60                     <include name="**/*Test.class"/>
    61                 </fileset>
    62             </batchtest>            
    63              
    64          </junit>
    65 
    66      </target>
    67 
    68     <!-- 清除Junit生成的报表文档 -->
    69     <target name="delete">
    70         <delete dir="${report.path}"/>
    71     </target>
    72     
    73 </project>

     3、使用<JunitReport>生成测试报告:

      在上面我们已经知道,通过formatter(type=“xml”)输出junit信息时会在指定目录下生成一个Test-类路径名.xml的xml文件,但是这个xml文件看起来很不方便。Ant提供了<junitreport>任务使用XSLT将xml文件转换为HTML报告,该任务首先将生成的XML文件整合成单一的XML文件,然后再对他进行转换,这个整合的文件默认情况下被命名为:TESTS-TestSuites.xml.

    • 修改上面的build.xml文件,增加65~71行,如下:
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <project name="AntDemo" default="junit" basedir=".">
     3     <!-- =================================================================== -->
     4     <!-- 变量设置  -->
     5     <!-- =================================================================== -->
     6 
     7     <!-- 源代码src路径 -->
     8     <property name="src.path" value="src/java"/>
     9     <!-- 编译文件class路径 -->
    10     <property name="build.path" value="build"/>
    11     <!-- 单元测试代码路径 -->
    12     <property name="test.path" value="src/test"/>
    13     <!-- lib包路径 -->
    14     <property name="lib.path" value="lib"/>
    15     <!-- 生成报告junit4.xml路径 -->
    16     <property name="report.path" value="report"/>
    17         
    18     <!-- =================================================================== -->
    19     <!-- 设置classpath -->
    20     <!-- =================================================================== -->
    21     <path id="compile.path">        
    22         <fileset dir="${lib.path}">
    23             <include name="**/*.jar"/>
    24         </fileset>
    25         
    26         <pathelement path="${build.path}"/>
    27     </path>     
    28 
    29     <target name="init">
    30         <mkdir dir="${build.path}"/>
    31         <mkdir dir="${report.path}"/>
    32     </target>
    33     
    34     <!-- =================================================================== -->
    35     <!-- 清除历史编译class -->
    36     <!-- =================================================================== -->
    37     <target name="clean" description="clean">
    38         <delete dir="${build.path}"/>
    39     </target>
    40 
    41     <!-- =================================================================== -->
    42     <!-- 编译测试文件,初始化目录 -->
    43     <!-- =================================================================== -->
    44     <target name="compile" depends="init" description="compile">
    45         <javac srcdir="${src.path}" destdir="${build.path}"  classpathref="compile.path"/>
    46         <javac srcdir="${test.path}" destdir="${build.path}"  classpathref="compile.path"/>
    47     </target>      
    48          
    49     <!-- =================================================================== -->
    50     <!-- 执行测试案例 -->
    51     <!-- =================================================================== -->
    52     <target name="junit" depends="compile">                
    53         <junit printsummary="true" fork="true">        
    54              <formatter type="xml" usefile="true"/>        
    55             
    56              <classpath refid="compile.path"/>        
    57             
    58             <batchtest fork="on" todir="${report.path}" haltonfailure="no">
    59                 <fileset dir="${build.path}">
    60                     <include name="**/*Test.class"/>
    61                 </fileset>
    62             </batchtest>                 
    63          </junit>
    64         
    65         <!-- 产生单元测试报表文档 -->
    66         <junitreport todir="${report.path}">
    67             <fileset dir="${report.path}">
    68                 <include name="TEST-*.xml" />
    69             </fileset>
    70             <report format="frames" todir="${report.path}" />
    71         </junitreport>
    72 
    73      </target>
    74 
    75     <!-- 清除Junit生成的报表文档 -->
    76     <target name="delete">
    77         <delete dir="${report.path}"/>
    78     </target>
    79     
    80 </project>

    执行后会在指定目录下生成报告文档,打开index.html可以很方便的看到执行的结果。

     1、如下图所示(我又补充了3个案例,并且故意让两个案例失败),显示执行的统计结果:

    2、点击classes下面的ComplexCalculationTest,可以看到具体某个类里面的单元测试案例执行统计情况以及失败案例的错误信息提示。

     

    OVER!

    参考:

    http://blog.csdn.net/shendl/article/details/532587

    http://blog.csdn.net/tochal/article/details/12560151

  • 相关阅读:
    2020.4.21 考试T1 HDU 5729
    BZOJ 4198: [Noi2015]荷马史诗
    BZOJ 1052: [HAOI2007]覆盖问题
    BZOJ 1087: [SCOI2005]互不侵犯King
    BZOJ 4466 线性函数
    Linux如何挂载U盘
    集中式日志分析平台
    ELK5.2+kafka+zookeeper+filebeat集群部署
    浅析ES的_source、_all、store、index
    IndexOf、LastIndexOf、Substring的用法
  • 原文地址:https://www.cnblogs.com/puresoul/p/4201565.html
Copyright © 2011-2022 走看看