Junit測試專案基礎

來源:互聯網
上載者:User

         Junit 3.8

 

 

1 .   在eclipse下面添加Junit的library

 

     右擊項目—》Build Path –》add library –》選擇Junit

 

2. 到官網下載 jar包,再匯入到項目即可。

 

3. 編寫測試案例。 在src同一等級下面建立src fold  命名為test。test下面建立的測試案例對應於要測試的類。如。 Student.java   --》studentTest.Java

 

4. Junit口號: keep the bar green  to keep the code clean

 

5. 單元測試不是為了證明你是對的,而是為了證明你沒有錯。    

 

6. 測試案例是單元測試的一個重要方面。

http://d.download.csdn.net/down/1653850/jerry2011

http://download.csdn.net/source/1653850#acomment  下載連結

 

7.  測試類別要繼承TestCase父類

     7.1 必須滿足的原則    public  void 無參數  方法名必須要以test開頭

 

8. TestCase之間一定要保持完全的獨立性,不允許出現任何的依賴關係。

 

9. 不能依賴於測試TestCase的順序。

 

10. DRY  (don’t  repeat  yourself)

 

11  一般先寫測試,後寫執行個體

 

12 public static void main(String[] args) { //Junit內建的運行器

         junit.textui.TestRunner.run(xx.class);       

    }

 

13 . 測試之前是什麼狀態,測試後就應該是什麼狀態。

 

 

14.  測試私人方法

      A. 修改方法的存取修飾詞

      B. 利用反射在測試類別中調用目標類地私人方法。(推薦)

 

15.   測試套件 TestSuite  可以將多個測試混合在一起,同時執行多個測試。

 

   建立一個TestAll類 ,加入:         

 

import junit.framework.TestCase;import junit.framework.TestSuite;public class TestAll extends TestCase{public static TestSuite suite(){     TestSuite suite = new TestSuite();  suite.addTestSuite(Calculator2Test.class);  suite.addTestSuite(DeleteAllTest.class);  suite.addTestSuite(CalculateTest.class);  return  suite; } }

 

 同時如何要對一個方法重複測試多次。可以如此

TestSuite suite = new TestSuite();  suite.addTestSuite(Calculator2Test.class);  suite.addTestSuite(DeleteAllTest.class);  suite.addTestSuite(CalculateTest.class);  //對calculate中add方法重複測試20次 suite.addTest(new  RepeatedTest(new CalculateTest("testAdd"),20));   return  suite;


 

    Junit4

 

16   Junit4 並沒用要求測試的類一定要繼承TestCase,可以引入Annotion。在方法名前面加入@Test.

 

如:

public class CalculatorJunit4Test{   private CalculatorJunit4 calculatorJunit4;   public CalculatorJunit4Test() {              calculatorJunit4 = new CalculatorJunit4();          } @org.junit.Test public void testAdd(){  int result = calculatorJunit4.add(2, 3); Assert.assertEquals(5, result); }}


 

 

17   雖然Junit4沒用要求方法名的格式,但是一定要有一個好的命名習慣。

        用@Before 和 @After 可以實現構建和銷毀,實現Junit3.8中setUp和tearDown方法。

 

 

 

 

18 用@Before Class 和@AfterClass 來定義一個全域的初始化方法。如資料庫的連結。

 

  

  @BeforeClass  public static void globalInit(){    System.out.println("globalInit()");    }  @AfterClass  public static void globalDestory(){    System.out.println("globalDestory()");    }

19.  利用@Test(timeout = 300) 可以判定一個方法是否逾時。

      利用@Test(expected = Exception.class)來判定是否有異常發生。

  利用@Ignore(”string”) 來申明一個方法還沒用準備好,不要做測試。也可以用在整個類上面。

 

如:

 @Test(timeout=3000)           //逾時3秒 則判定出錯  public void testAdd(){     int result = calculatorJunit4.add(2, 3);   Assert.assertEquals(5,result); }    @Ignore       @Test(expected = Exception.class)//期望拋出異常       public void testDivide() throws  Exception{      calculatorJunit4.divide(1, 0);   }

 

20 參數化測試(Parameters):當一個測試類別使用參數運行時,需要在類地前面聲明@Parameters註解來修飾,同時在構造方法中為參數賦值。最後編寫測試類別。

 

如:

 

import static org.junit.Assert.*;import java.util.Arrays;import java.util.Collection;import java.util.Collections;import org.junit.Assert;import org.junit.Before;import org.junit.Test;import org.junit.runner.RunWith;import org.junit.runners.Parameterized;import org.junit.runners.Parameterized.Parameters;@RunWith(Parameterized.class)public class ParametersTest {    private int expected ;    private int input1;    private int input2;    private CalculatorJunit4 cal;        @Parameters    public  static  Collection preparedData(){        Object  object[][] = {{5,2,3},{8,1,7},{0,-1,1},{20000,10000,10000}};    return  Arrays.asList(object);        }            public ParametersTest(int ex,int inp1,int inp2){     this.expected = ex ;     this.input1 = inp1;     this.input2 = inp2;            }            @Before    public   void setUp(){       System.out.println("setUp");      cal = new  CalculatorJunit4();    }        @Test    public void testAdd(){             assertEquals(this.expected, cal.add(this.input1, this.input2));       }}                     

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.