用Ant自動(dòng)測試JUnit
經(jīng)過痛苦的N次嘗試,終于,我成功的運(yùn)行了Ant驅(qū)動(dòng)的JUnit!
一、問題一 支持ANT的<junit>任務(wù)所需的jar包的配置。
Note: This task depends on external libraries not included in the Ant distribution. See Library Dependencies for more information.
注意:JUnit這個(gè)人物依賴于可選的庫,不包括在標(biāo)準(zhǔn)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,并且這個(gè)類文件必須在同一個(gè)ant路徑下
- Put both junit.jar and the optional tasks jar file in ANT_HOME/lib.
1,把junit.jar和可選任務(wù)jar(optional.jar) 文件放進(jìn)ANT_HOME/lib目錄中。 環(huán)境變量ANT_HOME = **\ant 這樣的目錄。
- Do not put either in ANT_HOME/lib, and instead include their locations in your CLASSPATH environment variable.
2,如果不把junit.jar和可選任務(wù)jar(optional.jar) 文件放進(jìn)ANT_HOME/lib目錄中,那么可以這樣做: 把這兩個(gè)jar文件的絕對(duì)路徑(包括文件名)寫進(jìn)你的系統(tǒng)環(huán)境變量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個(gè)辦法,那么,你可以這樣:
在構(gòu)造文件ant中,使用<classpath>元素,指定junit.jar和可選任務(wù)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>
上面這個(gè)例子,可以在cmd命令行中運(yùn)行。
如果要直接在Eclipse中運(yùn)行,則需要改變ant的設(shè)置。
使用eclipse可以按照一下步驟加入:
Windows-Preference-Ant-Runtime-Ant Home Entries
窗口—首選項(xiàng)—ant—運(yùn)行時(shí)—類路徑—Ant主目錄條目,然后添加外部jar。 主要添加我們ANT_HOME中的junit.jar這個(gè)文件即可。 實(shí)際不需要optional.jar這個(gè)文件。
因?yàn)椋?span lang="EN-US">org.apache.ant_
二、問題二 JUnit任務(wù)的classpath支持類路徑的設(shè)置的問題
這也是一個(gè)錯(cuò)誤點(diǎn)!
示例中是:
<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"/>
實(shí)際上,這就是我們在 <junit〉任務(wù)下, 我們使用了編譯后的.class文件的目錄,還有編譯所需的jar包所在的目錄。 缺一不可! 否則一定會(huì)報(bào)ClassNotFoundException類未找到異常!
因?yàn)椋?span lang="EN-US">JUnit任務(wù),實(shí)際就是為我們運(yùn)行Test類,而不僅僅是想我們的發(fā)布Ant文件那樣僅僅是javac 編譯,只需要編譯所需的Jar包。
我們還需要像java任務(wù)那樣運(yùn)行.class文件。 所以必須包括編譯后的.class文件。
OK!搞定這兩個(gè)問題后,我們就可以順利地自動(dòng)批量執(zhí)行JUnit測試了!
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=532587