cactus提供了cactus和RunServerTests两个task来运行测试。
"cactus" task是通过复制容器服务器的最小文件并运行来运行测试,因此需要制定容器服务器的类型,启动速度稍快点,另外配置比较方便,但是无法测试象tomcat连接池等资源。另外对tomcat5.5的支持也不好。
"RunServerTests"是通过直接启动容器服务起来运行测试,因此速度稍慢,且配置较麻烦,但能测试各种资源。
<target name="test" depends="test.prepare" description="Run tests on Tomcat "> <!-- Start the servlet engine, wait for it to be started, run the unit tests, stop the servlet engine, wait for it to be stopped. The servlet engine is stopped if the tests fail for any reason --> <!-- 8080是服务器的端口号,${project.name}-cactified是项目的路径,和上一步的cactifywar 的destfile相对应 --> <runservertests testURL="http://localhost:8080/${project.name}-cactified/ServletRedirector?Cactus_Service=RUN_TEST" startTarget="_StartTomcat" stopTarget="_StopTomcat" testTarget="_Test"/> </target>
<!-- _Test就是一个普通的junit任务 --> <target name="_Test"> <junit printsummary="yes" fork="yes"> <classpath> <path refid="project.classpath"/> <pathelement location="${target.classes.java.dir}"/> <pathelement location="${target.classes.test.dir}"/> </classpath> <formatter type="brief" usefile="false"/> <formatter type="xml"/> <batchtest> <fileset dir="${src.test.dir}"> <!-- Due to some Cactus synchronization bug, the 'unit' tests need to run before the 'sample' tests --> <include name="**/Test*.java"/> <exclude name="**/Test*All.java"/> </fileset> </batchtest> </junit> </target>