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类代码如下:

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:\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

 

时间: 2024-09-19 10:01:39

ANT task之Junit、JunitReport的相关文章

用Ant自动测试JUnit

一.问题一 支持ANT的<junit>任务所需的jar包的配置. Note: This task depends on external libraries not included in the Ant distribution. See Library Dependencies for more information. 注意:JUnit这个人物依赖于可选的库,不包括在标准ant.jar中. Note: You must have junit.jar and the class files

怎么为ant中的junit配制需要的xml文件?

问题描述 单元测试要用到配置数据库的xml文件,在ant配置junit中,怎么配制这些配置文件? 问题补充:yqin 写道 解决方案 http://blog.csdn.net/watson243671/archive/2010/02/15/5309247.aspxhttp://bbs.51testing.com/archiver/tid-18046.html

初学maven(4)-使用maven ant task实现非标准打包

maven很强大,但是总有些事情干起来不是得心应手,没有使用ant时那种想怎么干就怎么干的流畅感.尤其当要打包一个特殊(相对maven的标准架构而且)时,常有不知所措的感觉.当然这个应该和自己对maven的了解不够有关,毕竟,"初学maven"嘛. 但是maven在依赖管理方面实在是太强大了,太喜欢,退回原来的ant方式完全不可能,我想用过maven的人,一般是不会有回到原来在cvs,subversion中checkin/checkout n个jar包的时代,仅此一项理由就足够继续坚持

搭建持续集成单元测试平台(Jenkins+Ant+Java+Junit+SVN)

一.环境准备 Jenkins: 到官网下载jenkins.war包:http://jenkins-ci.org/ 安装方法有两种: 把下载下来的jenkins.war包放到文件夹下,如C:\jenkins,然后打开命令行窗口并进到该目录下,执行java -jar jenkens.war命令,当提示:"Jenkins is fully up and running"时,表示启动成功,这时在浏览器窗口输入:http://localhost:8080/ 就可到jenkins的首页. 如果有t

Ant+junit的测试自动化

随着Refactoring技术和XP软件工程技术的广泛推广,单元测试的作用在软件工程中变得越来越重要,而一个简明易学.适用广泛.高效稳定的单元测试框架则对成功的实施单元测试有着至关重要的作用.在java编程语句环境里,Junit Framework是一个已经被多数java程序员采用和实证的优秀的测试框架,但是多数没有尝试Junit Framework的 <?xml version="1.0" encoding="gb2312"?> <!--测试文件

在eclipse中使用Ant执行JUnit

建立工程: 在eclipse中建立一个java project. AntTest -src -com.test -ABS.java -ABSTest.java (Junit Test Case) -lib -build.xml -JUNIT_HOME/junit.jar配置: eclipse->window->preference->ANT->Runtime->Classpath->ANT Home Entry 选择Add External JAR,加入ECLIPSE_

上个项目中写的Ant build文件

项目 一.上一个项目中写的,可以编译.自动化测试.打包的build.xml <?xml version="1.0"?><project name="SimulatorEL" basedir="." default="all"><!-- edit by YangHuaWei --> <property name="build.dir" value="clas

关于ant跑覆盖率的问题

问题描述 现在有一个项目是struts+spring+ibatis的架构.需要用ant来批量跑junit和覆盖率,由于本人没有这方便的经验,一直没有调试出来.希望有此经验者不吝赐教.如果手头有以前项目的成品,或者类似的东西的也请共享下,谢谢了!现在编译是没有问题的,但是跑junit的时候case跑不过(eclipse下全成功.没有跑通的case好像都是一个原因:private CnxLogger logger = CnxLogger.getLogger(VMBHDM001LogicImpl.cl

关于 junitreport

问题描述 我在build.xml中配置如下:<?xmlversion="1.0"?><projectname="java-test2"><propertyname="src.dir"value="src"/><propertyname="test.dir"value="test"/><propertyname="report