單元測試是編寫測試代碼,應該準確、快速地保證程序基本模塊的正確性。
junit是java單元測試框架,已經在eclipse中默認安裝。
junit4
junit4通過注解的方式來識別測試方法。目前支持的主要注解有:
- @beforeclass 全局只會執行一次,而且是第一個運行
- @before 在測試方法運行之前運行
- @test 測試方法
- @after 在測試方法運行之后允許
- @afterclass 全局只會執行一次,而且是最后一個運行
- @ignore 忽略此方法
下面基于eclipse介紹junit的基本應用
基本測試
1.新建一個項目叫junittest,我們編寫一個calculator類,這是一個能夠簡單實現加減乘除、平方、開方的計算器類,然后對這些功能進行單元測試。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
public class calculator { private static int result; // 靜態變量,用于存儲運行結果 public void add( int n) { result = result + n; } public void substract( int n) { result = result - 1 ; //bug: 正確的應該是 result =result-n } public void multiply( int n) { } // 此方法尚未寫好 public void divide( int n) { result = result / n; } public void square( int n) { result = n * n; } public void squareroot( int n) { for (; ;) ; //bug : 死循環 } public void clear() { // 將結果清零 result = 0 ; } public int getresult(){ return result; } } |
1.將junit4單元測試包引入這個項目:在該項目上點右鍵,點“屬性”,如圖
在彈出的屬性窗口中,首先在左邊選擇“java build path”,然后到右上選擇“libraries”標簽,之后在最右邊點擊“add library…”按鈕,如下圖所示
然后在新彈出的對話框中選擇junit4并點擊確定,如上圖所示,junit4軟件包就被包含進我們這個項目了。
2.生成junit測試框架:在eclipse的package explorer中用右鍵點擊該類彈出菜單,選擇“new junit test case”。如下圖所示:
點擊“下一步”后,系統會自動列出你這個類中包含的方法,選擇你要進行測試的方法。此例中,我們僅對“加、減、乘、除”四個方法進行測試。
之后系統會自動生成一個新類calculatortest,里面包含一些空的測試用例。你只需要將這些測試用例稍作修改即可使用。
完整的calculatortest代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
public class calculatortest { private static calculator calculator = new calculator(); @before public void setup() throws exception { calculator.clear(); } @test public void testadd() { calculator.add( 3 ); calculator.add( 4 ); assertequals( 7 , calculator.getresult()); } @test public void testsubstract() { calculator.add( 8 ); calculator.substract( 3 ); assertequals( 5 , calculator.getresult()); } @ignore ( "multiply() not yet implemented" ) @test public void testmultiply() { fail( "not yet implemented" ); } @test public void testdivide() { calculator.add( 8 ); calculator.divide( 2 ); assertequals( 4 , calculator.getresult()); } } |
1.運行測試代碼:按照上述代碼修改完畢后,我們在calculatortest類上點右鍵,選擇“run as a junit test”來運行我們的測試,如下圖所示
運行結果如下:
進度條是紅顏色表示發現錯誤,具體的測試結果在進度條上面有表示“共進行了4個測試,其中1個測試被忽略,一個測試失敗”。
限時測試
對于那些邏輯很復雜,循環嵌套比較深的程序,很有可能出現死循環,因此一定要采取一些預防措施。限時測試是一個很好的解決方案。我們給這些測試函數設定一個執行時間,超過了這個時間,他們就會被系統強行終止,并且系統還會向你匯報該函數結束的原因是因為超時,這樣你就可以發現這些bug了。要實現這一功能,只需要給@test標注加一個參數即可,代碼如下:
1
2
3
4
5
|
@test (timeout = 1000 ) public void squareroot() { calculator.squareroot( 4 ); assertequals( 2 , calculator.getresult()); } |
timeout參數表明了你要設定的時間,單位為毫秒,因此1000就代表1秒。
測試異常
java中的異常處理也是一個重點,因此你經常會編寫一些需要拋出異常的函數。那么,如果你覺得一個函數應該拋出異常,但是它沒拋出,這算不算bug呢?這當然是bug,并junit也考慮到了這一點,來幫助我們找到這種bug。例如,我們寫的計算器類有除法功能,如果除數是一個0,那么必然要拋出“除0異常”。因此,我們很有必要對這些進行測試。代碼如下:
1
2
3
4
|
@test (expected = arithmeticexception. class ) public void dividebyzero(){ calculator.divide( 0 ); } |
如上述代碼所示,我們需要使用@test標注的expected屬性,將我們要檢驗的異常傳遞給他,這樣junit框架就能自動幫我們檢測是否拋出了我們指定的異常。
參數化測試
我們可能遇到過這樣的函數,它的參數有許多特殊值,或者說他的參數分為很多個區域。
例如,測試一下“計算一個數的平方”這個函數,暫且分三類:正數、0、負數。在編寫測試的時候,至少要寫3個測試,把這3種情況都包含了,這確實是一件很麻煩的事情。測試代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
public class advancedtest { private static calculator calculator = new calculator(); @before public void clearcalculator(){ calculator.clear(); } @test public void square1() { calculator.square( 2 ); assertequals( 4 , calculator.getresult()); } @test public void square2(){ calculator.square( 0 ); assertequals( 0 , calculator.getresult()); } @test public void square3(){ calculator.square(- 3 ); assertequals( 9 , calculator.getresult()); } } |
為了簡化類似的測試,junit4提出了“參數化測試”的概念,只寫一個測試函數,把這若干種情況作為參數傳遞進去,一次性的完成測試。代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
@runwith (parameterized. class ) public class squaretest{ private static calculator calculator = new calculator(); private int param; private int result; @parameters public static collection data() { return arrays.aslist( new object[][]{ { 2 , 4 }, { 0 , 0 }, {- 3 , 9 }, }); } //構造函數,對變量進行初始化 public squaretest( int param, int result){ this .param = param; this .result = result; } @test public void square(){ calculator.square(param); assertequals(result, calculator.getresult()); } } |
執行了3次該測試類,依次采用了數據集合中的數據{處理值,預期處理結果},結果如下:
代碼分析如下:
- 為這種測試專門生成一個新的類,而不能與其他測試共用同一個類,此例中我們定義了一個squaretest類。
- 為這個類指定一個runner,而不能使用默認的runner,@runwith(parameterized.class)這條語句就是為這個類指定了一個parameterizedrunner
- 定義一個待測試的類,并且定義兩個變量,一個用于存放參數,一個用于存放期待的結果。
- 定義測試數據的集合,也就是上述的data()方法,該方法可以任意命名,但是必須使用@parameters標注進行修飾。
- 定義構造函數,其功能就是對先前定義的兩個參數進行初始化
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://www.cnblogs.com/happyzm/p/6482886.html