junit单元测试

来源:互联网 发布:python 和 ruby比较 编辑:程序博客网 时间:2024/06/06 09:21

利用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()方法。

    protectedvoid setUp() throws Exception {
        super
.setUp();
        testPerson
=new Person("wxx","boy",175,22);
    }
//
tearDown()方法则是在每个测试方法之后,释放测试程序方法中引用的变量和实例
    protectedvoid 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() {
        TestSuitesuite
= new TestSuite("Test for com.wuxiaoxiao.junit");
        //$JUnit
-BEGIN$
        suite
.addTestSuite(PersonTest2.class);
        suite
.addTestSuite(PersonTest.class);
        //$JUnit
-END$
        returnsuite
;
    }

}

常用的断言方法:

assertEquals(a,b)

assertNotNull(a)

assertFalse(a):测试a是否为false

assertNull(a)

assertSame(a,b):测试ab是否=

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}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*.*" />
</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>

0 0
原创粉丝点击