您的位置:軟件測試 > 開源軟件測試 > 開源單元測試工具 > junit
Junit4測試
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時(shí)間:[ 2013/3/21 15:22:14 ] 推薦標(biāo)簽:

        完整的循環(huán)測試代碼如下:
Java代碼
import static org.junit.Assert.*; 
 
import java.util.Arrays; 
import java.util.Collection; 
 
import org.junit.AfterClass; 
import org.junit.BeforeClass; 
import org.junit.Ignore; 
import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.junit.runners.Parameterized; 
import org.junit.runners.Parameterized.Parameters; 
 
/**
 * @author bulargy.j.bai
 * @創(chuàng)建時(shí)間:Mar 11, 2008
 * @描述:
 */
@RunWith(Parameterized.class) 
public class MathTest { 
    int faciend; 
    int multiplicator; 
    int result; 
 
    public MathTest(int faciend, int multiplicator, int result) { 
         this.faciend = faciend; 
         this.multiplicator = multiplicator; 
         this.result = result; 
    } 
    /**
     * @throws java.lang.Exception
     */
    @BeforeClass
    public static void setUpBeforeClass() throws Exception { 
    } 
 
    /**
     * @throws java.lang.Exception
     */
    @AfterClass
    public static void tearDownAfterClass() throws Exception { 
    } 
 
    /**
     * Test method for {@link org.bj.util.Math#divide(int, int)}.
     */
    @Test(expected=ArithmeticException.class) 
    public void testDivide() { 
        assertEquals(3,Math.divide(9,3)); 
        assertEquals(3,Math.divide(10,3)); 
        Math.divide(10,0);//除數(shù)不能為0,會(huì)拋出異常 
 
    } 
 
    /**
     * Test method for {@link org.bj.util.Math#multiple(int, int)}.
     */
    //@Ignore("忽略乘法測試") 
    @Test
    public void testMultiple() { 
        assertEquals(result,Math.multiple(faciend,multiplicator)); 
    } 
     
    @Parameters
    public static Collection multipleValues() { 
     return Arrays.asList(new Object[][] { 
        {3, 2, 6 }, 
        {4, 3, 12 }, 
        {21, 5, 105 }, 
        {11, 22, 242 }, 
        {8, 9, 72 }}); 
    } 
 
}

import static org.junit.Assert.*;

import java.util.Arrays;
import java.util.Collection;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

/**
 * @author bulargy.j.bai
 * @創(chuàng)建時(shí)間:Mar 11, 2008
 * @描述:
 */
@RunWith(Parameterized.class)
public class MathTest {
 int faciend;
 int multiplicator;
 int result;

 public MathTest(int faciend, int multiplicator, int result) {
   this.faciend = faciend;
   this.multiplicator = multiplicator;
   this.result = result;
 }
 /**
  * @throws java.lang.Exception
  */
 @BeforeClass
 public static void setUpBeforeClass() throws Exception {
 }

 /**
  * @throws java.lang.Exception
  */
 @AfterClass
 public static void tearDownAfterClass() throws Exception {
 }

 /**
  * Test method for {@link org.bj.util.Math#divide(int, int)}.
  */
 @Test(expected=ArithmeticException.class)
 public void testDivide() {
  assertEquals(3,Math.divide(9,3));
  assertEquals(3,Math.divide(10,3));
  Math.divide(10,0);//除數(shù)不能為0,會(huì)拋出異常

 }

 /**
  * Test method for {@link org.bj.util.Math#multiple(int, int)}.
  */
 //@Ignore("忽略乘法測試")
 @Test
 public void testMultiple() {
  assertEquals(result,Math.multiple(faciend,multiplicator));
 }
 
 @Parameters
 public static Collection multipleValues() {
  return Arrays.asList(new Object[][] {
        {3, 2, 6 },
        {4, 3, 12 },
        {21, 5, 105 },
        {11, 22, 242 },
        {8, 9, 72 }});
 }

}
        OK,大功告成。測試看看吧,測試類跑了5次~~。

        大概這么多體會(huì)了,總得來說JUnit4以后測試還是很方便的,順便這個(gè)是僅僅是為了做例子,實(shí)際使用中由于JUnit4不再受命名的限制,所以應(yīng)該劃分更細(xì)粒度的測試來完成,一個(gè)方法的正確,異常,錯(cuò)誤及邊界數(shù)據(jù)完全可以分開來寫測試方法。由于大部分情況資源只用加載和釋放一次足夠,大大提高的測試的速度,再也不會(huì)有以前那樣點(diǎn)開測試然后去泡咖啡的情況出現(xiàn)了~~呵呵~~

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