Junit使用教程(三)

4、實例總結java

1. 參數化測試oop

有時一個測試方法,不一樣的參數值會產生不一樣的結果,那麼咱們爲了測試全面,會把多個參數值都寫出來並一一斷言測試,這樣有時不免費時費力,這是咱們即可以採用參數化測試來解決這個問題。參數化測試就比如把一個「輸入值,指望值」的集合傳入給測試方法,達到一次性測試的目的。測試

[java]  view plain copy
 
  1. package test;  
  2.   
  3. import static org.junit.Assert.*;  
  4.   
  5. import java.util.Arrays;  
  6.   
  7. import org.junit.Test;  
  8. import org.junit.runner.RunWith;  
  9. import org.junit.runners.Parameterized;  
  10. import org.junit.runners.Parameterized.Parameters;  
  11.   
  12. @RunWith(Parameterized.class)  
  13. public class FibonacciTest {  
  14.   
  15.     @Parameters(name = "{index}: fib({0})={1}")  
  16.     public static Iterable<Object[]> data() {  
  17.         return Arrays.asList(new Object[][] { { 0, 0 }, { 1, 1 }, { 2, 1 },  
  18.                 { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 } });  
  19.     }  
  20.   
  21.     private int input;  
  22.     private int expected;  
  23.   
  24.     public FibonacciTest(int input, int expected) {  
  25.         this.input = input;  
  26.         this.expected = expected;  
  27.     }  
  28.   
  29.     @Test  
  30.     public void test() {  
  31.         assertEquals(expected, Fibonacci.compute(input));  
  32.     }  
  33. }  
  34.   
  35. class Fibonacci {  
  36.   
  37.     public static int compute(int input) {  
  38.         int result;  
  39.         switch (input) {  
  40.         case 0:  
  41.             result = 0;  
  42.             break;  
  43.         case 1:  
  44.         case 2:  
  45.             result = 1;  
  46.             break;  
  47.         case 3:  
  48.             result = 2;  
  49.             break;  
  50.         case 4:  
  51.             result = 3;  
  52.             break;  
  53.         case 5:  
  54.             result = 5;  
  55.             break;  
  56.         case 6:  
  57.             result = 8;  
  58.             break;  
  59.         default:  
  60.             result = 0;  
  61.         }  
  62.         return result;  
  63.     }  
  64. }  

@Parameters註解參數name,實際是測試方法名稱。因爲一個test()方法就完成了全部測試,那假如某一組測試數據有問題,那在Junit的結果頁面裏該如何呈現?所以採用name實際上就是區分每一個測試數據的測試方法名。以下圖:ui

2. 打包測試this

一樣,若是一個項目中有不少個測試用例,若是一個個測試也很麻煩,所以打包測試就是一次性測試完成包中含有的全部測試用例。spa

[java]  view plain copy
 
  1. package test;  
  2.   
  3. import org.junit.runner.RunWith;  
  4. import org.junit.runners.Suite;  
  5.   
  6. @RunWith(Suite.class)  
  7. @Suite.SuiteClasses({ AssertTests.class, FibonacciTest.class, JDemoTest.class })  
  8. public class AllCaseTest {  
  9.   
  10. }  

這個功能也須要使用一個特殊的Runner ,須要向@RunWith註解傳遞一個參數Suite.class 。同時,咱們還須要另一個註解@Suite.SuiteClasses,來代表這個類是一個打包測試類。並將須要打包的類做爲參數傳遞給該註解就能夠了。至於AllCaseTest隨便起一個類名,內容爲空既可。運行AllCaseTest類便可完成打包測試.net

3. 異常測試blog

異常測試與普通斷言測試不一樣,共有三種方法,其中最爲靈活的是第三種,能夠與斷言結合使用教程

第一種:ip

[java]  view plain copy
 
  1. @Test(expected= IndexOutOfBoundsException.class)   
  2. public void empty() {   
  3.      new ArrayList<Object>().get(0);   
  4. }  

第二種:

[java]  view plain copy
 
  1. @Test  
  2. public void testExceptionMessage() {  
  3.     try {  
  4.         new ArrayList<Object>().get(0);  
  5.         fail("Expected an IndexOutOfBoundsException to be thrown");  
  6.     } catch (IndexOutOfBoundsException anIndexOutOfBoundsException) {  
  7.         assertThat(anIndexOutOfBoundsException.getMessage(), is("Index: 0, Size: 0"));  
  8.     }  
  9. }  

第三種:

[java]  view plain copy
 
  1. @Rule  
  2. public ExpectedException thrown = ExpectedException.none();  
  3.   
  4. @Test  
  5. public void shouldTestExceptionMessage() throws IndexOutOfBoundsException {  
  6.     List<Object> list = new ArrayList<Object>();  
  7.   
  8.     thrown.expect(IndexOutOfBoundsException.class);  
  9.     thrown.expectMessage("Index: 0, Size: 0");  
  10.     list.get(0);  
  11.     Assert.assertEquals(1, list.get(0));  
  12. }  

在上述幾種方法中,不管是expected仍是expect都表示指望拋出的異常,假如某一方法,當參數爲某一值時會拋出異常,那麼使用第一種方法就必須爲該參數單獨寫一個測試方法來測試異常,而沒法與其餘參數值一同寫在一個測試方法裏,因此顯得累贅。第二種方法雖然解決這個問題,可是寫法不只繁瑣也不利於理解。而第三種犯法,不只能動態更改指望拋出的異常,與斷言語句結合的也很是好,所以推薦使用該方法來測試異常。

4. 限時測試

有時爲了防止出現死循環或者方法執行過長(或檢查方法效率),而須要使用到限時測試。顧名思義,就是超出設定時間即視爲測試失敗。共有兩種寫法。

第一種:

[java]  view plain copy
 
  1. @Test(timeout=1000)  
  2. public void testWithTimeout() {  
  3.   ...  
  4. }  

第二種:

[java]  view plain copy
 
  1. public class HasGlobalTimeout {  
  2.     public static String log;  
  3.   
  4.     @Rule  
  5.     public Timeout globalTimeout = new Timeout(10000); // 10 seconds max per method tested  
  6.   
  7.     @Test  
  8.     public void testInfiniteLoop1() {  
  9.         log += "ran1";  
  10.         for (;;) {  
  11.         }  
  12.     }  
  13.   
  14.     @Test  
  15.     public void testInfiniteLoop2() {  
  16.         log += "ran2";  
  17.         for (;;) {  
  18.         }  
  19.     }  
  20. }  

其中,第二種方法與異常測試的第三種方法的寫法相似。也是推薦的寫法。

 

至此,Junit的教程總結性文章已介紹完了。經過系統總結也進一步加深了對Junit的認識,但願也能一樣幫助到對Junit還不太理解的朋友。若是你們還有什麼好的建議和用法,很歡迎能提出來一塊兒交流。

相關文章
相關標籤/搜索