Java单元测试,测试环境、自动执行脚本
如何进行单元测试,如何批量执行测试用例?
2011年 01 月 13 日
文章内容描述了单元测试环境以及单元测试自动构建脚本
单元测试
- [好处]
- 最早发现问题
- 利于软件维护
- [缺点]
- 麻烦、增加工作量
测试环境
- [工具包]
- JUnit4 单元测试框架
- HttpUnit 辅助进行Web单元测试
- Ant1.7.1 实现自动化构建
辅助单元测试
- [考虑测试资源]
- 数据源的绑定,像J2EE系统都会有一个Web容器,如Resin/Tomcat等,这些容器都会有自己的java.naming.InitialContextFactory实现,可以直接使用相关Web容器的JNDI实现完成数据源的绑定。
- [模块间的依赖关系]
- 这块可考虑扩展JUnit来实现,有篇文章就是讨论扩展JUnit主题,见参考资料。
自动构建脚本
目录结构
Ant脚本 (下载文件)
<target name="init">
<echo>Init Begin</echo>
<mkdir dir="${project.build.tmpdir}" />
<mkdir dir="${project.build.tmpdir.confdir}" />
<mkdir dir="${project.junit.output}" />
<echo>Init End</echo>
</target>
<target name="destroy">
<echo>Destroy Begin</echo>
<delete dir="${project.build.tmpdir}" />
<delete>
<fileset dir="${project.junit.output}">
<include name="TEST*.xml" />
</fileset>
</delete>
<echo>Destroy End</echo>
</target>
<target name="compileSRC">
<echo message="Compile SRC Begin" />
<javac srcdir="${project.basedir}/src" destdir="${project.build.tmpdir}">
<classpath refid="project.compile.lib" />
</javac>
<echo message="Compile SRC End" />
</target>
<target name="compileTEST">
<echo message="Compile TEST Begin" />
<javac srcdir="${project.basedir}/test" destdir="${project.build.tmpdir}" encoding="GBK">
<classpath refid="project.compile.lib" />
<classpath refid="project.compile.testlib" />
</javac>
<echo message="Compile TEST End" />
</target>
<target name="copyfile">
<echo message="BEGIN WEB-INF END" />
<copy todir="${project.build.tmpdir.confdir}">
<fileset dir="${project.webroot.confdir}">
<include name="*.xml" />
<include name="*.properties" />
<include name="*.log" />
</fileset>
</copy>
<echo message="Copy WEB-INF END" />
<echo message="Copy test config file BEGIN" />
<copy todir="${project.build.tmpdir}">
<fileset dir="${project.basedir}/test">
<include name="**/*.properties" />
</fileset>
</copy>
<echo message="Copy test config file END" />
</target>
<target name="testunit">
<echo message="Running TestCase Begin" />
<junit printsummary="on" fork="true" showoutput="true">
<classpath refid="project.compile.lib" />
<classpath refid="project.compile.testlib" />
<formatter type="xml" />
<batchtest todir="${project.junit.output}">
<fileset dir="${project.build.tmpdir}">
<patternset refid="project.junit.exclude" />
</fileset>
</batchtest>
</junit>
<echo message="Running TestCase End" />
</target>
<target name="report" depends="testunit">
<echo message="Report Begin" />
<junitreport todir="${project.junit.output}">
<fileset dir="${project.junit.output}">
<include name="TEST-*.xml" />
</fileset>
<report todir="${project.junit.output}" format="frames" />
</junitreport>
<echo message="Report End" />
</target>
<target name="openFile">
<taskdef name="browseURL" classname="com.hd.testunit.AntOpenCommand">
<classpath path="${classpath}" />
<classpath location="${project.build.tmpdir}" />
</taskdef>
<browseURL fileUrl="${project.junit.output}/index.html" />
</target>
<target name="compile" depends="compileSRC, compileTEST">
<echo message="Compile successful." />
<antcall target="copyfile" />
</target>
<target name="main" depends="init,compile,report,openFile,destroy">
<echo>
date:${date}
</echo>
</target>
</project>
|