程序測試中常見的有:白盒測試、黑盒測試、用例測試等。
這裏主要講用例測試:JUnit
在需要測試的代碼裏按住Alt鍵+Insert鍵,並選擇Test…
然後先下載JUnit插件
找到選擇想要下載哪個版本的插件 選擇ok就好
點擊OK進行測試類
但測試類會有部分報錯,需要再進行配置一下
有可能這裏會有報錯
主要原因:JUnit5版本針對測試的方法名稱有要求,
所有的測試方法必須是以“test”開頭,
因此,正確的測試類名爲testAdd、testSub
另外
@BeforeAll
public static void beforeAll(){
System.out.println("所有測試之前的調用");
}
@AfterAll
public static void afterAll(){
System.out.println("所有測試之後的調用");
}
@BeforeEach
public void beforeEach(){
System.out.println("每個測試之前的調用");
}
@AfterEach
public void afterEach(){
System.out.println("每個測試之後的調用");
}
利用Order 自定義測試代碼的執行順序
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class MyMathTest {
@BeforeAll
public static void beforeAll(){
System.out.println("所有測試之前的調用");
}
@AfterAll
public static void afterAll(){
System.out.println("所有測試之後的調用");
}
@BeforeEach
public void beforeEach(){
System.out.println("每個測試之前的調用");
}
@AfterEach
public void afterEach(){
System.out.println("每個測試之後的調用");
}
@Order(2)
@org.junit.jupiter.api.Test
void testAdd() {
System.out.println("***加法測試***");
assertEquals(MyMath.add(101,202), 303);
}
@Order(1)
@org.junit.jupiter.api.Test
void testSub() {
System.out.println("***減法測試***");
assertEquals(MyMath.sub(202,201), 1);
}
}
執行結果