Junit4學習總結
Junit4在工作中一直只關注簡單的單元測試應用,并沒有進行系統一點的了解。這兩天在修改測試用例時,對它進行了一些了解。現把自己的想法記錄下來,以便加深記憶和日后查看。
Junit4.x后的jar包中,存在兩類:org.junit.*和junit.framework.*
其中,
junit.framework.*是Junit4之前的包,需要繼承TestCase類實現單元測試類的編寫,且每個測試方法都要求以testxxx規則命名,Junit框架才能運行該測試方法。
org.junit.*是Junit4之后的包,不再需要繼承任何類,只需要使用注釋(@Test)即可實現測試方法的自動調用,測試方法命名也自定義,不需再遵守testxxx的命名規則。
Junit的各測試方法的執行順序不是按代碼中的順序執行,順序無法指定,不同的運行平臺執行順序也會有差異。
以下是針對以上兩種Junit的使用示例:
1.Junit4.x版本以前的測試方法:
package com.self;import junit.framework.TestCase;
public class TestJunitCase extends TestCase {
public TestJunitCase(String methodName) { super(methodName); } /** * 測試方法1 */ public void testFirst() { System.out.println("first Test Case..."); } /** * 測試方法2 */ public void testSecond() { System.out.println("second Test Case..."); } /** * 測試方法3 */ public void testThird() { System.out.println("third Test Case..."); }
}</pre>
測試結果如下:
first Test Case... second Test Case... third Test Case...提示:若需要對測試用例的執行順序進行指定,可通過以下方法進行調整。(建議各測試用例保持相對獨立,盡量解耦,減少依賴及不必要的影響)
package com.self;import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite;
public class TestJunitCase extends TestCase {
public TestJunitCase(String methodName) { super(methodName); } <strong><span style="color:#ff0000;">/** * 可通過該方法調整各測試方法的執行順序 * @return */ </span><span style="color:#000000;">public static Test suite() { TestSuite suite = new TestSuite(); suite.addTest(new TestJunitCase("testSecond")); suite.addTest(new TestJunitCase("testThird")); suite.addTest(new TestJunitCase("testFirst")); return suite; }</span></strong> /** * 測試方法1 */ public void testFirst() { System.out.println("first Test Case..."); } /** * 測試方法2 */ public void testSecond() { System.out.println("second Test Case..."); } /** * 測試方法3 */ public void testThird() { System.out.println("third Test Case..."); }
}</pre>
測試結果如下:
second Test Case... third Test Case... first Test Case...2.Junit4.x版本以后的測試方法:
package com.self;import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test;public class TestPractise {public static int i; public static int j; public TestPractise() { super(); } /** * 只調用一次,在所有測試方法執行前調用,可做些準備工作 */ @BeforeClass public <strong><span style="color:#ff0000;">static</span></strong> void beforeClass() { System.out.println("this method is called only One Time before..."); } /** * 每個測試方法執行前都調用 */ @Before public void beforeMethod() { System.out.println("this method is called EveryTime before..." + i++); } /** * 測試方法1 */ @Test public void firstMethod() { System.out.println("first Test Case..."); } /** * 測試方法2 */ @Test public void secondMethod() { System.out.println("second Test Case..."); } /** * 每個測試方法執行后都調用 */ @After public void afterMethod() { System.out.println("this method is called EveryTime end..." + j++); } /** * 只調用一次,在所有測試方法執行結束后調用,可做些清理工作 */ @AfterClass public <strong><span style="color:#ff0000;">static</span></strong> void afterClass() { System.out.println("this method is called only One Time after..."); }
}</pre>
測試結果如下:
this method is called only One Time before... this method is called EveryTime before...0 first Test Case... this method is called EveryTime end...0 this method is called EveryTime before...1 second Test Case... this method is called EveryTime end...1 this method is called only One Time after...備注:以上標紅的兩處,即@BeforeClass,@AfterClass必須是靜態方法
其他知識可參考以下兩處:
JUnit學習筆記:http://wanqiufeng.blog.51cto.com/409430/426763
全面認識JUnit4的新特特征:http://dev.yesky.com/375/2584375.shtml
轉自:http://binglimeng.iteye.com/blog/1416556