JUnit单元测试
TestSuite suite=new TestSuite("Test for com.wuxiaoxiao.junit");
//$JUnit-BEGIN$
suite.addTestSuite(PersonTest2.class);
suite.addTestSuite(PersonTest.class);
<junit printsummary="on"
haltonfailure="false"
failureproperty="tests.failed"
showoutput="true">
<classpath refid="master-classpath"/>
<formatter type="plain"/>
<bottom><![CDATA[<i>All Rights Reserved.</i>]]></bottom>
<!--<tag name="todo"scope="all"description="To do:"/>-->
</javadoc>
</target>
</project>
上面的可以作为一个模板(doc,report,log不要在webRoot下面建立)
JUnit单元测试
将JUnit.jar添加到lib中!
文件:
junit.zip
大小:
114KB
下载:
下载
利用eclipse自带的junit编写测试类
利用ant进行测试运行,以及生成报告文件
例子:
packagecom.wuxiaoxiao.junit;
publicclassPerson{
privateStringname;
this.high=high;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
=name;
}
publicStringgetSex(){
returnsex;
}
publicvoidsetSex(Stringsex){
<mkdir dir="${doc.dir}"/>
<javadoc destdir="${doc.dir}"
author="true"
version="true"
use="true"
windowtitle="TechTiger_Test_API">
<packageset dir="${src.dir}"defaultexcludes="yes">
protected void setUp()throws Exception {
super.setUp();
testPerson=new Person("wxx","boy",175,22);
}
//tearDown()方法则是在每个测试方法之后,释放测试程序方法中引用的变量和实例
protected void tearDown()throws Exception {
</fail>
</target>
<!-- 打包成jar -->
<!--
<target name="pack"depends="test"description="make .jar file">
<mkdir dir="${dist.dir}"/>
<jar destfile="${dist.dir}/hello.jar"basedir="${classes.dir}">
<batchtest todir="${report.dir}">
<fileset dir="${classes.dir}">
<include name="**/AllTests.class"/>
</fileset>
</batchtest>
</junit>
<fail if="tests.failed">
</target>
<!-- 编译 -->
<target name="compile"depends="init"description="compile the source files">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${src.dir}"destdir="${classes.dir}">
//$JUnit-END$
return suite;
}
}
常用的断言方法:
assertEquals(a,b)
assertNotNull(a)
assertFalse(a):测试a是否为false
assertNull(a)
assertSame(a,b):测试a和b是否=
assertTrue(a)
结合ant进行junit测试:
privateStringsex;
privateinthigh;
privateintage;
publicPerson(Stringname,Stringsex,inthigh,intage){
=name;
this.sex=sex;
this.high=high;
this.age=age;
<exclude name="com/junit/**"/>
<!--<include name="com/wuxiaoxiao/junit/**"/>-->
</packageset>
<doctitle><![CDATA[<h1>Ant_Junit_</h1>]]></doctitle>
}
publicintgetAge(){
returnage;
}
publicbooleansetAge(intage){
if(age>25)
returnfalse;
this.age=age;
returntrue;
}
publicintgetHigh(){
returnhigh;
}
publicvoidsetHigh(inthigh){
<!-- 定义classpath -->
<path id="master-classpath">
<fileset file="${lib.dir}/*.jar"/>
<pathelement path="${classes.dir}"/>
</path>
<!-- 初始化任务 -->
<target name="init">
<classpath refid="master-classpath"/>
</javac>
</target>
<!-- 测试 -->
<target name="test"depends="compile"description="run junit test">
<mkdir dir="${report.dir}"/>
}
publicvLeabharlann id testSetAge(){assertTrue(testPerson.setAge(21));
}
publicvoid testGetHigh(){
assertNotNull(testPerson.getHigh());
}
publicvoid testGetName(){
assertEquals(testPerson.getName(),"wxx");
<exclude name="**/*Test.*"/>
<exclude name="**/Test*.*"/>
</jar>
</target>
-->
<!-- 输出api文档 -->
<target name="doc"depends="test"description="create api doc">
this.sex=sex;
}
}
测试类1(可以单独执行):
package com.wuxiaoxiao.junit;