我们创建一个测试项目,名字为:test,并创建两个源文件夹:src和test..把项目源文件和测试文件分开放在这两个文件夹中。
我们在src编写一个测试类:
<pre name= "code" class = "java" > package com.widetrust; public class CountService { private int summary; private int count; public boolean stat( int visitor){ count++; summary += visitor; if (summary> 1000 && count> 2 ){ return true ; }else { return false ; } } }
在test文件夹写个测试该类的类:
<pre name= "code" class = "java" > package com.widetrust.test; import com.widetrust.CountService; import junit.framework.TestCase; public class TestCountService extends TestCase { CountService cs; protected void setUp() throws Exception { cs = new CountService(); } protected void tearDown() throws Exception { } public void testStat(){ assertEquals(true , cs.stat( 4000 )); } public void testStat2(){ cs.stat(2000 ); cs.stat(2000 ); assertEquals(true , cs.stat( 3000 )); } }
当然我们可以利用eclipse的Run As Junit Test(快捷键:Alt+Shift+X T)运行我们刚写好的测试案例,但有个问题是eclipse不能正确的同步我们的开发和测试程序,而且,我们可能还希望测试的过程中为我们提供一份详细的报告文档,以供我们在测试后的改进过程中的讨论。现在我们使用ant的自动编译功能来实现测试的自动化,并让它生成一份详细的测试报告。
注意的是,ant自带了junit的支持包,但项目需要junit.jar,还需要在项目的lib文件夹中放置junit.jar,(这里我假设工程目录存放项目依赖组件的是lib文件夹)如果我们要单独使用ant编译整个项目,需要在项目构建文件build.xml中定义编译所用到的lib包,
在项目的根目录创建一个build.xml,内容如下:
< pre name = "code" class = "html" > <? xml version = "1.0" ?> < project name = "ant and junit" default = "test auot junit and report" basedir = "." > < property name = "lib.dir" value = "lib" /> < path id = "classpath" > < fileset dir = "${lib.dir}" includes = "**/*.jar" /> </ path > < property name = "output folder" value = "classes" /> < property name = "src folder" value = "src" /> < property name = "test folder" value = "test" /> < property name = "report folder" value = "report" /> < target name = "clean" > < delete dir = "report" /> < echo > 清除测试报告文件 成功! </ echo > </ target > < target name = "compile init" > < mkdir dir = "${output folder}" /> < echo > 创建编译文件夹 成功! </ echo > </ target > < target name = "report init" depends = "clean" > < mkdir dir = "${report folder}" /> < echo > 创建测试报告文件夹 成功! </ echo > </ target > < target name = "compile" depends = "compile init" > < javac srcdir = "${src folder}" destdir = "${output folder}" classpathref = "classpath" /> < echo > 项目源文件编译 成功! </ echo > </ target > < target name = "test compile" depends = "report init" > < javac srcdir = "${test folder}" destdir = "${output folder}" classpathref = "classpath" /> < echo > 项目测试文件编译 成功! </ echo > </ target > < target name = "all compile" depends = "compile, test compile" > </ target > < target name = "test auot junit and report" depends = "all compile" > < junit printsummary = "on" fork = "true" showoutput = "true" > < classpath > < fileset dir = "${lib.dir}" includes = "**/*.jar" /> < pathelement path = "${output folder}" /> </ classpath > < formatter type = "xml" /> < batchtest todir = "${report folder}" > < fileset dir = "${output folder}" > < include name = "**/Test*.*" /> </ fileset > </ batchtest > </ junit > < junitreport todir = "${report folder}" > < fileset dir = "${report folder}" > < include name = "TEST-*.xml" /> </ fileset > < report format = "frames" todir = "${report folder}" /> </ junitreport > </ target > </ project >
我们在eclipse中利用windows
->
show View
->
Ant 打开ant工作窗口,点击”Add Buildfiles” 将项目的根目录下的build.xml添加进去,然后在ant工作窗口点运行图标,可以看到控制台上的输出:(当然,用命令行方式运行也可以)
整个项目编译成功,并在项目根目录生成一个report文件夹,我们可以从中看到一份详细的测试报告:
< span style = "background-color: rgb(255, 255, 255);" > 现在我们已经可以做到测试代码和开发代码分开,并利用ant的自动编译功能为我们执行测试计划和生成测试报告。虽然在编写测试案例的时候确实比较麻烦,我们需要详细的模拟实际应用中的环境,并寻找可能出现错误的边界值帮助我们在编写实际代码时尽量减少隐患的发生,往往编写测试案例的时间比我们编写实际代码的时间还长,但这样做是非常值得的,一个软件的开发周期最长的60%都是花在寻找错误和完善的过程中。 </ span > < span style = "font-family:Lucida Grande, Verdana, Arial, sans-serif;color:#333333;" > < span style = "font-size: 13.3333330154419px; line-height: 20px;" > </ span > </ span >