您的位置:軟件測試 > 開源軟件測試 > 開源單元測試工具 > junit
JUnit的基本使用
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時間:[ 2014/11/19 15:25:20 ] 推薦標(biāo)簽:JUnit 軟件測試 單元測試

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void info()
{
System.out.println("the stu info:"+this.age+" "+this.id+" "+this.name);
}
}
package com.test.unittest;
public class Teacher {
String tname;
String tage;
public Teacher(String tname, String tage) {
super();
this.tname = tname;
this.tage = tage;
}
public String getTname() {
return tname;
}
public void setTname(String tname) {
this.tname = tname;
}
public String getTage() {
return tage;
}
public void setTage(String tage) {
this.tage = tage;
}
public void info(){
System.out.println("the teacher info:"+this.tage+" " +this.tname);
}
}
  后面這部分是對兩個類進(jìn)行的單元測試以及一個組合方式的使用
package com.Unittest;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.test.unittest.Student;
public class StudentTest {
Student stu=new Student(1,23,"令狐沖");
@Before
public void setUp(){
System.out.println("Student Initial");
}
@Test
public void infoTest()
{
stu.info();
}
@After
public void tearDown(){
System.out.println("Student Destroy");
}
}
package com.Unittest;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.test.unittest.Teacher;
public class TeacherTest {
Teacher teacher=new Teacher("風(fēng)清揚(yáng)","90");
@Before
public void setUp(){
System.out.println("Teacher Initial");
}
@Test
public void infoTest()
{
teacher.info();
}
@After
public void tearDown(){
System.out.println("Teacher Destroy");
}
}
package com.Unittest;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
import com.test.unittest.Student;
@RunWith(Suite.class)
@SuiteClasses({StudentTest.class,TeacherTest.class})
public class AllTest {
}
/*輸出的結(jié)果如下:
Student Initial
the stu info:23 1 令狐沖
Student Destroy
Teacher Initial
the teacher info:90 風(fēng)清揚(yáng)
Teacher Destroy
*/
  補(bǔ)充說明:
  寫作業(yè)的時候把測試類一個一個手敲進(jìn)去,真是太out了,還是用eclipse中自帶的生成JUnit test的類比較好一點(diǎn),直接在測試的那個package下面,創(chuàng)建一個新的JUnit Test Class 選定版本以及選定class under test 這個表示你希望生成哪一個類的測試類,這樣生成的測試類中命名也比較規(guī)范,比如相同的方法名不同參數(shù)的方法,連參數(shù)類型都寫上去了,比以前直接用a b c d...1 2 3 4....來區(qū)別同名的方法正規(guī)多了....

上一頁12下一頁
軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd