将JUnit.jar添加到lib中!
|
文件: |
junit.zip |
大小: |
114KB |
下载: |
下载 | |
利用eclipse自带的junit编写测试类
利用ant进行测试运行,以及生成报告文件
例子:
package com.wuxiaoxiao.junit;
public class Person { private String name; private String sex; private int high; private int age; public Person(String name,String sex,int high,int age){ this.name=name; this.sex=sex; this.high=high; this.age=age; } public int getAge(){ return age; } public boolean setAge(int age){ if(age>25) return false; this.age=age; return true; } public int getHigh(){ return high; } public void setHigh(int high){ this.high=high; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; }
}
|
测试类1(可以单独执行):
package com.wuxiaoxiao.junit;
import junit.framework.TestCase;
public class PersonTest extends TestCase { Person testPerson;
//setUp()方法集中初始化测试所需的所有变量和实例,并且在依次调用测试类中的每个测试方法之前再次执行setUp()方法。
protected void setUp() throws Exception { super.setUp(); testPerson=new Person("wxx","boy",175,22); } //tearDown()方法则是在每个测试方法之后,释放测试程序方法中引用的变量和实例 protected void tearDown() throws Exception { }
public void testSetAge() { assertTrue(testPerson.setAge(21)); }
public void testGetHigh() { assertNotNull(testPerson.getHigh()); }
public void testGetName() { assertEquals(testPerson.getName(),"wxx"); }
}
|
测试类2(可以单独执行):
package com.wuxiaoxiao.junit;
import junit.framework.TestCase;
public class PersonTest2 extends TestCase { Person testPerson; protected void setUp() throws Exception { super.setUp(); testPerson=new Person("wxx","boy",175,22); }
protected void tearDown() throws Exception { super.tearDown(); }
public void testGetHigh() { assertNotNull(testPerson.getHigh()); }
}
|
测试套件(多个测试类的组合,依次执行):
package com.wuxiaoxiao.junit;
import junit.framework.Test; import junit.framework.TestSuite;
public class AllTests {
public static Test suite() { TestSuite suite = new TestSuite("Test for com.wuxiaoxiao.junit"); //$JUnit-BEGIN$ suite.addTestSuite(PersonTest2.class); suite.addTestSuite(PersonTest.class); //$JUnit-END$ return suite; }
}
|
常用的断言方法:
assertEquals(a,b)
assertNotNull(a)
assertFalse(a):测试a是否为false
assertNull(a)
assertSame(a,b):测试a和b是否=
assertTrue(a)
结合ant进行junit测试:
<?xml version="1.0" encoding="gb2312"?> <project name="Hello world" default="doc">
<!-- properies --> <property name="src.dir" value="src" /> <property name="report.dir" value="report" /> <property name="classes.dir" value="bin" /> <property name="lib.dir" value="lib" /> <!-- <property name="dist.dir" value="dist" />--> <property name="doc.dir" value="doc"/>
<!-- 定义classpath --> <path id="master-classpath"> <fileset file="${lib.dir}/*.jar" /> <pathelement path="${classes.dir}"/> </path>
<!-- 初始化任务 --> <target name="init"> </target>
<!-- 编译 --> <target name="compile" depends="init" description="compile the source files"> <mkdir dir="${classes.dir}"/> <javac srcdir="${src.dir}" destdir="${classes.dir}"> <classpath refid="master-classpath"/> </javac> </target>
<!-- 测试 --> <target name="test" depends="compile" description="run junit test"> <mkdir dir="${report.dir}"/> <junit printsummary="on" haltonfailure="false" failureproperty="tests.failed" showoutput="true"> <classpath refid="master-classpath" /> <formatter type="plain"/> <batchtest todir="${report.dir}"> <fileset dir="${classes.dir}"> <include name="**/AllTests.class"/> </fileset> </batchtest> </junit> <fail if="tests.failed"> *********************************************************** **** One or more tests failed! Check the output ... **** *********************************************************** </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}"> <exclude name="**/*Test.*" /> <exclude name="**/Test*.*" /> </jar> </target> --> <!-- 输出api文档 --> <target name="doc" depends="test" description="create api doc"> <mkdir dir="${doc.dir}" /> <javadoc destdir="${doc.dir}" author="true" version="true" use="true" windowtitle="TechTiger_Test_API"> <packageset dir="${src.dir}" defaultexcludes="yes"> <exclude name="com/junit/**" /> <!-- <include name="com/wuxiaoxiao/junit/**" />--> </packageset> <doctitle><![CDATA[<h1>Ant_Junit_Techtiger.cn</h1>]]></doctitle> <bottom><![CDATA[<i>All Rights Reserved.</i>]]></bottom> <!-- <tag name="todo" scope="all" description="To do:" />--> </javadoc> </target> </project>
|
上面的可以作为一个模板(doc,report,log不要在webRoot下面建立)
阅读(2075) | 评论(0) | 转发(0) |