ANT task之Junit、JunitReport

简介:

一、ANT任务之Junit:

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

Testing Tasks  
Task Name Description
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类代码如下:

  SimpleCalculation

 

3、测试类SimpleCalculationTest代码如下:

  SimpleCalculationTest

 

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

  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:\AntTest\build.xml
 2 clean:
 3    [delete] Deleting directory D:\AntTest\build
 4 compile:
 5     [mkdir] Created dir: D:\AntTest\build  
 6     [javac] Compiling 1 source file to D:\AntTest\build
 7    
 8     [javac] Compiling 1 source file to D:\AntTest\build
 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

 


本文转自贺满博客园博客,原文链接:http://www.cnblogs.com/puresoul/p/4201565.html,如需转载请自行联系原作者。

目录
相关文章
|
Java 测试技术 Apache
Android 使用Ant+Emma+Junit收集android代码覆盖率
完成Android项目相关代码研发后,有时要求进行代码覆盖率测试,这里提出一种解决方案。
168 0
|
Java jenkins 测试技术
Jenkins ANT+JUNIT实例(学习笔记二十七)
转载:http://www.cnblogs.com/helloIT/p/5761740.html 1.环境   windows10 专业版   Jenkins 2.7   apache-ant-1.9.6   JDK1.8.0_20 2.整体思路   jdk:支持ant脚本中的编译、测试等target。
1280 0
|
4天前
|
XML Java 测试技术
Spring5入门到实战------17、Spring5新功能 --Nullable注解和函数式注册对象。整合JUnit5单元测试框架
这篇文章介绍了Spring5框架的三个新特性:支持@Nullable注解以明确方法返回、参数和属性值可以为空;引入函数式风格的GenericApplicationContext进行对象注册和管理;以及如何整合JUnit5进行单元测试,同时讨论了JUnit4与JUnit5的整合方法,并提出了关于配置文件加载的疑问。
Spring5入门到实战------17、Spring5新功能 --Nullable注解和函数式注册对象。整合JUnit5单元测试框架
|
3月前
|
Java 测试技术 程序员
junit单元测试
junit单元测试
|
2月前
|
Java Maven Spring
spring如何使用junit进行测试
spring如何使用junit进行测试
spring如何使用junit进行测试
|
2月前
|
运维 Java 测试技术
Spring运维之boo项目表现层测试加载测试的专用配置属性以及在JUnit中启动web服务器发送虚拟请求
Spring运维之boo项目表现层测试加载测试的专用配置属性以及在JUnit中启动web服务器发送虚拟请求
31 3
|
2月前
|
Java 测试技术 持续交付
Springboot中JUNIT5单元测试+Mockito详解
Springboot中JUNIT5单元测试+Mockito详解
211 1
|
1月前
|
IDE Java 测试技术
Java面试题:描述在Java中使用JUnit进行单元测试的过程
Java面试题:描述在Java中使用JUnit进行单元测试的过程
23 0