您的位置:軟件測試 > 開源軟件測試 > 開源單元測試工具 > junit
JUnit4單元測試簡單入門
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時間:[ 2016/7/14 11:48:30 ] 推薦標(biāo)簽:單元測試 軟件測試工具

  在類名上面點擊右鍵生成test測試類


  測試類生成截圖


測試類名,勾選測試方法

  會在測試類的包中自動生成測試類
package com.example.news;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Created by 小新 on 2016/7/1.
*/
public class CalculatorTest {
@Before
public void setUp() throws Exception {
}
@Test
public void testSum() throws Exception {
}
@Test
public void testSubstract() throws Exception {
}
@Test
public void testDivide() throws Exception {
}
@Test
public void testMultiply() throws Exception {
}
@Test
public void testAddMore() throws Exception {
}
}
  然后進行測試
package com.example.news;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Created by 小新 on 2016/7/1.
*/
public class CalculatorTest1 {
private Calculator calculator;
@Before
public void setUp() throws Exception {
calculator = new Calculator();
}
@Test
public void testSum() throws Exception {
//測試sum函數(shù),因為sum函數(shù)返回的是兩個數(shù)的合為3
//這里期望返回的值是9
//所以會報錯
assertEquals(9d,calculator.sum(1d,2d),0);
}
@Test
public void testSubstract() throws Exception {
}
@Test
public void testDivide() throws Exception {
}
@Test
public void testMultiply() throws Exception {
}
@Test
public void testAddMore() throws Exception {
}
}
  運行這個類,結(jié)果為報錯,這個時候我們需要修改函數(shù)了
  到這里我們JUnix的簡單測試結(jié)束了

上一頁12下一頁
軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd