码迷,mamicode.com
首页 > 其他好文 > 详细

ANT task之Junit

时间:2015-01-04 19:04:49      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:

一、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 task,<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

 

 

 

参考:

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

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

 

ANT task之Junit

标签:

原文地址:http://www.cnblogs.com/puresoul/p/4201565.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!