Java单元测试,测试环境、自动构建脚本

Java单元测试,测试环境、自动执行脚本

如何进行单元测试,如何批量执行测试用例?

WangYanCheng (wangyancheng@ushayden.com), 作者/编者, ushayden, Inc

2011年 01 月 13 日

文章内容描述了单元测试环境以及单元测试自动构建脚本

单元测试

[好处]
最早发现问题
利于软件维护
[缺点]
麻烦、增加工作量

测试环境

[工具包]
JUnit4 单元测试框架
HttpUnit 辅助进行Web单元测试
Ant1.7.1 实现自动化构建

辅助单元测试

[考虑测试资源]
数据源的绑定,像J2EE系统都会有一个Web容器,如Resin/Tomcat等,这些容器都会有自己的 java.naming.InitialContextFactory实现,可以直接使用相关Web容器的JNDI实现完成数据源的绑定。
[模块间的依赖关系]
这块可考虑扩展JUnit来实现,有篇文章就是讨论扩展JUnit主题,见 参考资料

自动构建脚本

目录结构

目录结构

Ant脚本(下载文件)

<?xml version="1.0" encoding="UTF-8"?> <project default="main"> <tstamp> <format property="date" pattern="yyyy-MM-dd hh:mm:ss" locale="zh" /> </tstamp> <property name="project" value="TestUnit" /> <property name="project.basedir" value="D:/work/workspace/mywork" /> <property name="project.build" value="${project.basedir}/build" /> <property name="project.build.lib" value="${project.build}/lib" /> <property name="project.build.tmpdir" value="${project.build}/tmp" /> <property name="project.build.tmpdir.confdir" value="${project.build}/tmp/WEB-INF" /> <property name="project.webroot" value="${project.basedir}/WebContent" /> <property name="project.webroot.confdir" value="${project.webroot}/WEB-INF" /> <property name="project.webroot.lib" value="${project.webroot.confdir}/lib" /> <property name="project.junit.output" value="${project.webroot}/junit" /> <!--global conf--> <path id="project.compile.lib"> <pathelement path="${classpath}" /> <fileset dir="${project.webroot.lib}"> <include name="**/*.jar" /> </fileset> </path> <path id="project.compile.testlib"> <pathelement path="${project.build.tmpdir}" /> <fileset dir="${project.build.lib}"> <include name="**/*.jar" /> </fileset> </path> <patternset id="project.junit.exclude"> <include name="**/*Test*" /> <exclude name="**/testunit/**" /> <exclude name="**/chart/**" /> <exclude name="**/workflow/**" /> <exclude name="**/TestAction*" /> </patternset> <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>



回页首

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值