Junit隨記

頂級的開發人員素養
在idea中,可以方便的使用ctrl+shift+T,在選擇類名後,生成對應的Junit類
在需要測試的方法上加上 @Test

按照以前Junit3.x的遺留的規範,
測試類的命名,以Test結尾
方法命名,以test開頭

在junit,可以使用import static 靜態導入,節約不少代碼
主要需要導入的類有
import static org.junit.Assert.*; //傳統的Junit , Assert下的靜態方法
import static org.hamcrest.core.*; //junit4.x導入的外部類庫,主要提供assertThat方法的使用
import static org.junit.matchers.JUnitMatchers.*;
import static org.hamcrest.core.Is.is; //包含了Is方法,同級類下同樣也有類似的方法可以導入使用

假設的使用
assumeThat

註釋 @Theory 指定的帶參數的測試函數
junit原來的測試方法下,都是不帶參數的,使用@Theory註解,用於提供了使用數據進行測試的能力
@DataPoint 用於定義參數的數據源,junit會根據方法的參數數量,對原有定義的datapoint進行隨機組合後進行調用

測試方法異常的方式
@Test(expected=IndexOutOfBoundsException.class)
方法超時
@Test(timeout=1)
忽略測試方法
@Ignore("this regular expression isn't working yet")
@Test

@Before 和 @After 用於和 @BeforeClass 和 @AfterClass一起用於提供測試的固件(測試數據)

測試套件的annotation使用
@RunWith(Suite.class)
@SuiteClasses({ParametricRegularExpressionTest.class,
RegularExpressionTest.class,
TimedRegularExpressionTest.class})
class 之上
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章