Junit4學習(三)Junit執行流程

2022-02-11 03:09:14 字數 1432 閱讀 4884

一,驗證junit測試方法的流程

1,在test/com.duo.util右鍵,新建測試類

2,生成後的**:

1

package

com.duo.util;23

import

static org.junit.assert.*;45

import

org.junit.after;

6import

org.junit.afterclass;

7import

org.junit.before;

8import

org.junit.beforeclass;

9import

org.junit.test;

1011

public

class

junitflowtest

1718

@afterclass

19public

static

void teardownafterclass() throws

exception

2223

@before

24public

void setup() throws

exception

2728

@after

29public

void teardown() throws

exception

3233

@test

34public

void

test1()

3738

@test

39public

void

test2()

4243 }

執行結果:

this is @beforeclass...

this is before...

this is test1...

this is after...

this is before...

this is test2...

this is after...

this is afterclass...

二,總結:

1,@beforeclass修飾的方法會在所有方法被呼叫前被執行;而且該方法是靜態的,所以當測試類被載入後接著就會執行它,而且在記憶體中它只會存在乙份例項,它比較適合載入配置檔案;比如資料的連線檔案等;

2,@afterclass所修飾的方法通常用來對資源的清理,如資料庫的關閉;

3,@before和@after會在每個測試方法前後執行;通常被稱為固定**(fixure),就是一定會執行的**.

Junit4學習(五)Junit4測試套件

一,背景 1,隨著開發規模的深入和擴大,專案或越來越大,相應的我們的測試類也會越來越多 那麼就帶來乙個問題,假如測試類很多,就需要多次執行,造成測試的成本增加 此時就可以使用junit批量執行測試類的功能,junit test suite,測試套件 每次執行測試類,只需要執行一次測試套件類就可以執行...

JUnit4學習總結

在需要測試的類上進行新建junit類,放在測試的目錄下,以及確保測試目錄的包名和被測試類的包名一致。將所有的方法進行選擇 junit中 測試的三種狀態 junit的測試流程 junit的註解 test 將乙個普通的方法修飾成為乙個測試方法 test expected xx.class 捕捉異常 te...

JUnit4學習筆記1

在計算機程式設計中,單元測試 又稱為模組測試,unit testing 是針對程式模組 軟體設計的最小單位 來進行正確性檢驗的測試工作。程式單元是應用的最小可測試部件。對於物件導向程式設計,最小單元就是方法,包括基類 超類 抽象類 或者派生類 子類 中的方法。執行單元測試,是為了證明某段 的行為確實...