用Ant自动测试JUnit
经过痛苦的N次尝试,终于,我成功的运行了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 for the <junit> task in the same classpath. You can do one of:
注意:你必须拥有junit.jar,并且这个类文件必须在同一个ant路径下
- Put both junit.jar and the optional tasks jar file in ANT_HOME/lib.
1,把junit.jar和可选任务jar(optional.jar) 文件放进ANT_HOME/lib目录中。 环境变量ANT_HOME = **\ant 这样的目录。
- Do not put either in ANT_HOME/lib, and instead include their locations in your CLASSPATH environment variable.
2,如果不把junit.jar和可选任务jar(optional.jar) 文件放进ANT_HOME/lib目录中,那么可以这样做: 把这两个jar文件的绝对路径(包括文件名)写进你的系统环境变量CLASSPATH中。
- Do neither of the above, and instead, specify their locations using a <classpath> element in the build file. See the FAQ for details.
3,如果你不想做上面那2个办法,那么,你可以这样:
在构造文件ant中,使用<classpath>元素,指定junit.jar和可选任务jar(optional.jar) 文件的位置。
下面是成功的例子:
<?xml version="1.0"?>
<project name="project" default="junit">
<property name="run.classpath" value="bin"></property>
<property name="run.srcpath" value="src"></property>
<property name="test.srcpath" value="test"></property>
<property name="test.report" value="report"></property>
<property name="lib.dir" value="lib"/>
<path id="compile.path">
<fileset dir="${lib.dir}">
<include name="**/*.jar"/>
</fileset>
</path>
<!-- Compilation Classpath
<path id="compile.classpath">
<fileset dir="${telecom_LDBS.lib}">
<include name="**/*.jar"/>
</fileset>
</path>
-->
<target name="compile">
<javac destdir="${run.classpath}" srcdir="${run.srcpath}"
classpathref="compile.path"/>
<javac destdir="${run.classpath}" srcdir="${test.srcpath}"
classpathref="compile.path"/>
</target>
<target name="junit" depends="compile">
<tstamp/>
<mkdir dir="${test.report}"/>
<mkdir dir="${test.report}/framework-${DSTAMP}-${TSTAMP}"/>
<junit printsummary="true">
<classpath >
<pathelement path="${run.classpath}"/>
<fileset dir="${lib.dir}">
<include name="**/*.jar"/>
</fileset>
</classpath>
<!--
<test name="Ldbs1AdslSectionBaseinfoModelServiceTest"></test>
-->
<formatter type="plain"/>
<!-- she zhi yao ce shi de wen jian ji he .-->
<batchtest fork="yes" todir="${test.report}/framework-${DSTAMP}-${TSTAMP}">
<fileset dir="${test.srcpath}">
<include name="**/*Test.java"/>
</fileset>
</batchtest>
</junit>
</target>
</project>
上面这个例子,可以在cmd命令行中运行。
如果要直接在Eclipse中运行,则需要改变ant的设置。
使用eclipse可以按照一下步骤加入:
Windows-Preference-Ant-Runtime-Ant Home Entries
窗口—首选项—ant—运行时—类路径—Ant主目录条目,然后添加外部jar。 主要添加我们ANT_HOME中的junit.jar这个文件即可。 实际不需要optional.jar这个文件。
因为,org.apache.ant_
二、问题二 JUnit任务的classpath支持类路径的设置的问题
这也是一个错误点!
示例中是:
<junit printsummary="true">
<classpath>
<pathelement path="${run.classpath}"/>
<fileset dir="${lib.dir}">
<include name="**/*.jar"/>
</fileset>
</classpath>
其中
<property name="run.classpath" value="bin"></property>
<property name="lib.dir" value="lib"/>
实际上,这就是我们在 <junit〉任务下, 我们使用了编译后的.class文件的目录,还有编译所需的jar包所在的目录。 缺一不可! 否则一定会报ClassNotFoundException类未找到异常!
因为,JUnit任务,实际就是为我们运行Test类,而不仅仅是想我们的发布Ant文件那样仅仅是javac 编译,只需要编译所需的Jar包。
我们还需要像java任务那样运行.class文件。 所以必须包括编译后的.class文件。
OK!搞定这两个问题后,我们就可以顺利地自动批量执行JUnit测试了!
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=532587