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还真得快点跑啊。
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还真得快点跑啊。
相关文章推荐
- 使用Ant运行JUnit测试用例时,报java.lang.ClassNotFoundException: org.hamcrest.SelfDescribing的一种解决方案
- Jenkins+Ant+Java+Junit+SVN使用总结
- 使用Ant进行Junit测试
- 使用ant编译、运行junit、以及检查编码
- 使用Travis CI进行持续化集成(涉及Ant+Junit)
- Ant和Junit使用的一些问题
- 使用ant编译、运行junit、以及检查编码
- 使用ant编译、运行junit、以及检查编码
- 使用Ant+JUnit+Cobertura来实现代码覆盖自动测试
- 使用Ant进行Junit测试
- JAVA使用到的一些环境变量,Tomcat,Junit,Ant
- 使用ant进行junit测试
- 利用 Ant 和 JUnit 进行增量开发-使用单元测试来逐步改进代码
- Ant 使用Junit自动测试要注意的问题
- 在ant中使用junit命令跑所有测试
- 使用JUnit4.1时报错java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing错误
- 使用ant 跑junit test 生成 report
- 基于JUnit和Ant测试程序正在运行使用Kieker(AspectJ)监测方法
- vi使用、Ant安装配置、Junit和Sonar的学习心得
- 使用Ant与Junit进行自动化测试