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

本文转自http://blog.csdn.net/ybygjy/article/details/6135915

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

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

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

2011年 01 月 13 日

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

单元测试

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

测试环境

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

辅助单元测试

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

自动构建脚本

目录结构

目录结构

Ant脚本  (下载文件)

  1. <? xml   version = "1.0"   encoding = "UTF-8" ?>   
  2. < project   default = "main" >   
  3.     < tstamp >   
  4.         < format   property = "date"   pattern = "yyyy-MM-dd hh:mm:ss"   locale = "zh"   />   
  5.     </ tstamp >   
  6.     < property   name = "project"   value = "TestUnit"   />   
  7.     < property   name = "project.basedir"   value = "D:/work/workspace/mywork"   />   
  8.     < property   name = "project.build"   value = "${project.basedir}/build"   />   
  9.     < property   name = "project.build.lib"   value = "${project.build}/lib"   />   
  10.     < property   name = "project.build.tmpdir"   value = "${project.build}/tmp"   />   
  11.     < property   name = "project.build.tmpdir.confdir"   value = "${project.build}/tmp/WEB-INF"   />   
  12.     < property   name = "project.webroot"   value = "${project.basedir}/WebContent"   />   
  13.     < property   name = "project.webroot.confdir"   value = "${project.webroot}/WEB-INF"   />   
  14.     < property   name = "project.webroot.lib"   value = "${project.webroot.confdir}/lib"   />   
  15.     < property   name = "project.junit.output"   value = "${project.webroot}/junit"   />   
  16.     <!--global conf-->   
  17.     < path   id = "project.compile.lib" >   
  18.         < pathelement   path = "${classpath}"   />   
  19.         < fileset   dir = "${project.webroot.lib}" >   
  20.             < include   name = "**/*.jar"   />   
  21.         </ fileset >   
  22.     </ path >   
  23.     < path   id = "project.compile.testlib" >   
  24.         < pathelement   path = "${project.build.tmpdir}"   />   
  25.         < fileset   dir = "${project.build.lib}" >   
  26.             < include   name = "**/*.jar"   />   
  27.         </ fileset >   
  28.     </ path >   
  29.     < patternset   id = "project.junit.exclude" >   
  30.         < include   name = "**/*Test*"   />   
  31.         < exclude   name = "**/testunit/**"   />   
  32.         < exclude   name = "**/chart/**"   />   
  33.         < exclude   name = "**/workflow/**"   />   
  34.         < exclude   name = "**/TestAction*"   />   
  35.     </ patternset >   
  36.   
  37.     < target   name = "init" >   
  38.         < echo > Init Begin </ echo >   
  39.         < mkdir   dir = "${project.build.tmpdir}"   />   
  40.         < mkdir   dir = "${project.build.tmpdir.confdir}"   />   
  41.         < mkdir   dir = "${project.junit.output}"   />   
  42.         < echo > Init End </ echo >   
  43.     </ target >   
  44.     < target   name = "destroy" >   
  45.         < echo > Destroy Begin </ echo >   
  46.         < delete   dir = "${project.build.tmpdir}"   />   
  47.         < delete >   
  48.             < fileset   dir = "${project.junit.output}" >   
  49.                 < include   name = "TEST*.xml"   />   
  50.             </ fileset >   
  51.         </ delete >   
  52.         < echo > Destroy End </ echo >   
  53.     </ target >   
  54.     < target   name = "compileSRC" >   
  55.         < echo   message = "Compile SRC Begin"   />   
  56.         < javac   srcdir = "${project.basedir}/src"   destdir = "${project.build.tmpdir}" >   
  57.             < classpath   refid = "project.compile.lib"   />   
  58.         </ javac >   
  59.         < echo   message = "Compile SRC End"   />   
  60.     </ target >   
  61.     < target   name = "compileTEST" >   
  62.         < echo   message = "Compile TEST Begin"   />   
  63.         < javac   srcdir = "${project.basedir}/test"   destdir = "${project.build.tmpdir}"   encoding = "GBK" >   
  64.             < classpath   refid = "project.compile.lib"   />   
  65.             < classpath   refid = "project.compile.testlib"   />   
  66.         </ javac >   
  67.         < echo   message = "Compile TEST End"   />   
  68.     </ target >   
  69.     < target   name = "copyfile" >   
  70.         < echo   message = "BEGIN WEB-INF END"   />   
  71.         < copy   todir = "${project.build.tmpdir.confdir}" >   
  72.             < fileset   dir = "${project.webroot.confdir}" >   
  73.                 < include   name = "*.xml"   />   
  74.                 < include   name = "*.properties"   />   
  75.                 < include   name = "*.log"   />   
  76.             </ fileset >   
  77.         </ copy >   
  78.         < echo   message = "Copy WEB-INF END"   />   
  79.         < echo   message = "Copy test config file BEGIN"   />   
  80.         < copy   todir = "${project.build.tmpdir}" >   
  81.             < fileset   dir = "${project.basedir}/test" >   
  82.                 < include   name = "**/*.properties"   />   
  83.             </ fileset >   
  84.         </ copy >   
  85.         < echo   message = "Copy test config file END"   />   
  86.     </ target >   
  87.     < target   name = "testunit" >   
  88.         < echo   message = "Running TestCase Begin"   />   
  89.         < junit   printsummary = "on"   fork = "true"   showoutput = "true" >   
  90.             < classpath   refid = "project.compile.lib"   />   
  91.             < classpath   refid = "project.compile.testlib"   />   
  92.             < formatter   type = "xml"   />   
  93.             < batchtest   todir = "${project.junit.output}" >   
  94.                 < fileset   dir = "${project.build.tmpdir}" >   
  95.                     < patternset   refid = "project.junit.exclude"   />   
  96.                 </ fileset >   
  97.             </ batchtest >   
  98.         </ junit >   
  99.         < echo   message = "Running TestCase End"   />   
  100.     </ target >   
  101.     < target   name = "report"   depends = "testunit" >   
  102.         < echo   message = "Report Begin"   />   
  103.         < junitreport   todir = "${project.junit.output}" >   
  104.             < fileset   dir = "${project.junit.output}" >   
  105.                 < include   name = "TEST-*.xml"   />   
  106.             </ fileset >   
  107.             < report   todir = "${project.junit.output}"   format = "frames"   />   
  108.         </ junitreport >   
  109.         < echo   message = "Report End"   />   
  110.     </ target >   
  111.     < target   name = "openFile" >   
  112.         < taskdef   name = "browseURL"   classname = "com.hd.testunit.AntOpenCommand" >   
  113.             < classpath   path = "${classpath}"   />   
  114.             < classpath   location = "${project.build.tmpdir}"   />   
  115.         </ taskdef >   
  116.         < browseURL   fileUrl = "${project.junit.output}/index.html"   />   
  117.     </ target >   
  118.     < target   name = "compile"   depends = "compileSRC, compileTEST" >   
  119.         < echo   message = "Compile successful."   />   
  120.         < antcall   target = "copyfile"   />   
  121.     </ target >   
  122.     < target   name = "main"   depends = "init,compile,report,openFile,destroy" >   
  123.         < echo >   
  124.             date:${date}  
  125.         </ echo >   
  126.     </ target >   
  127. </ project >   
 

猜你喜欢

转载自xst4002.iteye.com/blog/1543830
今日推荐