您的位置:首页 > 其它

ant中使用junit4.1

2006-11-26 20:41 309 查看
想在ant中使用junit4还是很麻烦的。因为ant1.6.5最新发布的版本不支持junit4。在ANT的网站看到ant1.7可以支持了。下了1.7RC1的源码编译一下。生成了相应的jar。
1、 将生成的bin目录和lib 目录放到一个目录。并把这个目录设置成ANT_HOME(不设置也可以,主要是为了方便),然后将ANT_HOME/bin加入path中。
2、将junit4.1.jar复制到ANT_HOME/lib目录下。
这个如果不加入的话,用ANT编译的时候就会提示找不到org.junit
加入之后。在Eclipse中运行ANT任务中的编译,还是提示找不到org.junit。找不到原因,只好在命令行中使用ANT了。
3、在一个junit4风格的测试(在Eclipse下很方便的啊)
4、单独运行(不用ANT)测试用例,成功,证明测试可以工作了。
5、在build.xml中加入<junit>和<junitreport>
完整的build.xml代码如下:


<?xml version="1.0"?>


<!-- ======================================================================


2006-11-26 下午03:38:02


libo


====================================================================== -->


<project name="JA" default="default">


<!-- =================================


define variable


================================= -->




<property name="src.dir" location="src">


</property>


<property name="src.java.dir" location="${src.dir}/main/java" />


<property name="src.test.dir" location="${src.dir}/test/java" />


<property name="target.dir" location="target" />


<property name="target.java.dir" location="${target.dir}/classes" />


<property name="target.test.dir" location="${target.dir}/test-classes" />


<property name="target.report.dir" location="${target.dir}/report">


</property>




<!-- =================================


target: default


================================= -->


<target name="default" depends="report" description="--> description">




</target>




<!-- - - - - - - - - - - - - - - - - -


target: complie java


- - - - - - - - - - - - - - - - - -->


<target name="complie.java">


<mkdir dir="${target.java.dir}" />


<javac destdir="${target.java.dir}">


<src path="${src.java.dir}">


</src>


</javac>


</target>


<!-- - - - - - - - - - - - - - - - - -


target: complie test-java


- - - - - - - - - - - - - - - - - -->


<target name="complie.test" depends="complie.java">


<mkdir dir="${target.test.dir}" />


<javac destdir="${target.test.dir}">


<src path="${src.test.dir}">


</src>


<classpath>


<pathelement location="${target.java.dir}" />


</classpath>


</javac>


</target>


<!-- - - - - - - - - - - - - - - - - -


target: complie java and test-java


- - - - - - - - - - - - - - - - - -->




<target name="complie" depends="complie.java,complie.test">


</target>




<!-- - - - - - - - - - - - - - - - - -


target: test


- - - - - - - - - - - - - - - - - -->


<target name="test" depends="complie">


<mkdir dir="${target.report.dir}" />


<junit printsummary="yes" haltonerror="yes" haltonfailure="yes" fork="yes">


<formatter type="plain" usefile="flase" />


<formatter type="xml" />


<test name="t.MainClassTest" todir="${target.report.dir}">


</test>


<classpath>


<pathelement location="${target.java.dir}" />


<pathelement location="${target.test.dir}" />


<pathelement path="${java.class.path}" />


</classpath>


</junit>


</target>


<target name="report" depends="test">


<mkdir dir="${target.report.dir}/html"/>


<junitreport todir="${target.report.dir}">


<fileset dir="${target.report.dir}">


<include name="TEST-*.xml"/>


</fileset>


<report todir="${target.report.dir}/html"/>


</junitreport>


</target>


</project>



执行ant complie 没有问题,但是执行 ant test 就会出现在错误。经过一系列的查找与比较。最后发现是ant1.7RC1的源码是的build.xml在执行的时候并没有把org.apache.tools.ant.taskdefs.optional.junit包下的类文件加入ant-junit.jar包中。只好通压缩软件加这个包下的所有class文件加入ant-junit.jar中。
现在执行ant report就可以了。

另外又发现一个问题是:
只有所有的测试都成功的时候才会生成report。否则就不会生成。这样有点不好吧。。。看来unit4还真得快点跑啊。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: