[問題] 撰寫 junit Test Suite 的正確方法
各位有使用 junit 的板大們好
想請問到底寫 junit 4 test suite 的正確方法是什麼呢?
過去我一直找不到 junit 有系統又好懂的教學文章,
因此寫 test suite 的方法是網路上隨便找程式碼,自己亂試土法煉鋼搞出來的.. QQ
因為我要寫的 test suite 都是針對相同 test case 類別給不同參數做測試,
所以土法試出來的做法是在預計要測試的 test case 類別,
除了無參數建構式以外, 再額外寫一個有參數的建構式,
然後 test suite 這樣寫 :
@RunWith(value=Parameterized.class)
public class MyTestSuite extends MyTestCase {
public MyTestSuite(String acct, String pw) {
super(acct, pw);
}
@Parameterized.Parameters
public static Collection<String[]> getParameters() {
/*在此設定不同 Test Case 要給入的參數*/
return collection;
}
}
這方法在我用 eclipse Helios 的時候,不管單獨執行 Test Case 或是跑 Test suite
都能用,後來改用 eclipse Indigo 時,不曉得是我沒弄清楚 junit 4 的規則,
或是 eclipse 的 junit 套件之使用規則
現在相同的做法會使得 Test case 無法單獨呼叫 junit 測試,
一開始就直拋出錯誤 :
java.lang.IllegalArgumentException: Test class can only have one constructor
但是 Test suite 卻又能跑,我想請問為什麼會這樣呢?
一直到現在,舊有寫好的 Test Case Test Suite 都能跑,
但如果將舊的 Test Case 裡面 import org.junit.Test 移除,
再透過 eclipse 的提示功能自動載入 org.junit.Test,
就會出現跟上述情況一樣問題.....
對了,附帶一提 不曉得會這樣的原因是不是我 Test case 的寫法有錯?
我的 test 通常都是這樣寫,因為喜歡這樣的直觀俐落︰
import junit.framework.Assert;
import org.junit.Test;
@Test
public void myTest() {
Assert.assertNotNull(xxxxx);
}
可是後來發現這樣寫好像是綜合 junit 3 與 junit 4 函式庫的寫法,
不知道這樣的寫法是不是錯誤的原因呢?
謝謝大家指教
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 122.116.39.240
※ 編輯: dream1124 來自: 122.116.39.240 (12/19 04:37)
討論串 (同標題文章)