JUnit4之隱藏特性”曝光”

來源:互聯網
上載者:User

  只要做Java開發的,99%的都用過JUnit。在JUnit4裡面除了@Test, @Before, @BeforeClass
這樣基本的特性,JUnit4還有些很酷但是大家平時不太用的特性。我這裡做一下介紹,大家可以用一下,或者至少把玩一下:) 以下特性都基於4.8版本。

 

  • ParallelComputer: 並行測試

當需要同時測試testcase的時候,你不需要為自己寫thread來做這個事情。JUnit4已經有了這個特性,而且還提供多種同步選擇。例如:

/**<br /> * @author 盧聲遠<michaellufhl@yahoo.com.cn><br /> */<br />public class ParallelComputerTest {</p><p> @Test<br /> public void test() {<br /> Class[] cls={ParallelTest1.class,ParallelTest2.class };</p><p> //Parallel among classes<br /> JUnitCore.runClasses(ParallelComputer.classes(), cls);</p><p> //Parallel among methods in a class<br /> JUnitCore.runClasses(ParallelComputer.methods(), cls);</p><p> //Parallel all methods in all classes<br /> JUnitCore.runClasses(new ParallelComputer(true, true), cls);<br /> }<br /> public static class ParallelTest1{<br /> @Test public void a(){}<br /> @Test public void b(){}<br /> }<br /> public static class ParallelTest2{<br /> @Test public void a(){}<br /> @Test public void b(){}<br /> }<br />}

你有3種同步方式:

1: ParallelComputer.classes():所有測試類別同時開始執行,但是每個類裡面的方法還是順序執行。在例子裡面ParallelTest1
ParallelTest2
同時開始,但是各自的a(),b()
還是順序執行。

2: ParallelComputer.methods():測試類別順序執行,但是每個類裡面的方法是同時開始執行。在例子裡面ParallelTest1
a()
b()
同時開始,等結束之後再開始ParallelTest2

3: new ParallelComputer(true, true):所有測試類別裡面方法同時開始執行。在例子裡面4個方法同時開始執行。

很有意思吧。

 

  • Category: 分類測試

當你有很多testcase,但是你不想每次都執行一遍的時候。你可以把testcase分成若干類,然後就可以分類有選擇的來執行這些testcase。

例子:譬如你有2類testcase,一類是重要的用Important.class表示,還有一類不那麼重要的用Secondary.class表示。

/**<br /> * @author 盧聲遠<michaellufhl@yahoo.com.cn><br /> */<br />interface Important{};<br />interface Secondary{};<br />@RunWith(Categories.class)<br />@IncludeCategory(Important.class)<br />@ExcludeCategory(Secondary.class)<br />@SuiteClasses( { CategoryTest.Alpha.class,<br /> CategoryTest.Beta.class })<br />public class CategoryTest {</p><p> @Category(Important.class)<br /> public static class Alpha{//Alpha is Important except b<br /> @Test<br /> public void a(){}</p><p> @Test<br /> @Category(Secondary.class)<br /> public void b(){}<br /> }</p><p> public static class Beta{<br /> @Test<br /> @Category(Important.class)<br /> public void a(){}//a is Important<br /> }<br />}<br />

當你執行後會發現只有Alpha.a()
Beta.a()
執行到,Alpha.b()
@ExcludeCategory
掉了。

 

  • MaxCore: 失敗優先

  當需要反覆測試大量testcase的時候,你往往比較關注那些前一次失敗的case。這個時候你就可以利用MaxCore特性在每次執行testcase的時候優先執行那些上次失敗的case。

例如:

/**<br /> * @author 盧聲遠<michaellufhl@yahoo.com.cn><br /> */<br />public class MaxCoreTest {</p><p> private MaxCore fMax;</p><p> @Before<br /> public void createMax() {<br /> File fMaxFile= new File("MaxCore.ser");<br /> fMax= MaxCore.storedLocally(fMaxFile);<br /> }</p><p> @Test<br /> public void test(){<br /> Request request= Request.aClass(Delta.class);<br /> fMax.run(request);<br /> fMax.run(request);//b->c->a<br /> }</p><p> public static class Delta{<br /> @Test public void a(){}<br /> @Test public void b(){<br /> Assert.fail();<br /> }<br /> @Test public void c(){}<br /> }<br />}

可以觀察到第2次執行的時候Delta.b()
由於第1次的失敗而提前到了最先執行。

 

 

  • DataPoint: 參數資料

當需要測試不同輸入參數的時候是不是要寫一大堆testcase?不需要,因為JUnit4提供了參數化測試資料的特性。例如你想要測試2個整型資料作為輸入參數的時候:

/**<br /> * @author 盧聲遠<michaellufhl@yahoo.com.cn><br /> */<br />@RunWith(Theories.class)<br />public class DataPointTest {<br /> @DataPoint<br /> public static int ONE= 1;<br /> @DataPoint<br /> public static int TWO= 2;</p><p> /* You could also use @DataPoints instead of @DataPoint<br /> @DataPoints<br /> public static int[] points= { 1, 2 };*/</p><p> @Theory<br /> public void test(int first, int second) {<br /> }<br />}

通過觀察test()
被執行了4次,每次不同的組合作為參數輸入。而且如果參數數目過多,你可以通過@DataPoints
一下子把參數都設進去。用過@RunWith(Parameterized.class)
的朋友可以和DataPoint做一下比較。

  • TestedOn

這個功能也是參數化資料,只不過它是直接在參數上配置。例如:

@Theory<br />public void test2(@TestedOn(ints={1,2})int first) {<br /> System.out.println(first);<br />}

結果列印出來"1
2"

 

 

  • PrintableResult: 列印結果

這個我認為倒是一個小功能,它只是把測試失敗的結果以比較美觀的格式打出來。例如:

Result result = JUnitCore.runClasses(SomeTest.class);<br />System.out.println(new PrintableResult(result.getFailures()));

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.