任何人都遇到了這個錯誤測試類只能有一個構造
initializationError(junit.framework.TestSuite):測試類只能有一個構造
使用情況。
我在TestSuite中有2個TestScripts。如果我動態創建TestSuite,然後添加2個testScript並調用它,其中clazz是TestSuite類。
但是,如果每調用2個TestScripts上的JUnitCore.runClasses,就沒有問題。
我使用了JUnit3。
我對這個JUnit error - IllegalArgumentException:Test class can only have one constructor有類似的問題,但我的動態包括創建測試套件類。原因是有一個用例可以在測試套件中包含1個測試腳本,或者我可以在測試套件中包含2個測試腳本。
更新代碼:
public class ScriptTest1 extends TestCase {
private ScriptTest1() { }
public void testMethod1() {
Assert.assertEquals(true, true);
}
}
public class ScriptTest2 extends TestCase {
private ScriptTest2() { }
public void testMethod2() {
Assert.assertEquals(true, true);
}
}
然後在另一個類
File file = new File("file to ScriptTest.class");
Class<?> clazz = Class.forName(className, true, Thread.currentThread().getContextClassLoader());
//construct a new test suite
TestSuite ts = new TestSuite("Sample Test Suite with only ScriptTest1");
ts.addTestSuite((Class<? extends TestCase>) clazz);
Result result = JUnitCore.runClasses(ts.getClass());
HashSet<String> failureMethod = new HashSet<String>();
for (Failure failure : result.getFailures()) {
System.out.println("Failure: " + failure.toString());
}
失敗將輸出:initializationError(junit.framework.TestSuite):測試類只能有一個構造 但是,如果我只是直接做到這一點,它會運行良好。
File file = new File("file to ScriptTest.class");
Class<?> clazz = Class.forName(className, true, Thread.currentThread().getContextClassLoader());
Result result = JUnitCore.runClasses(clazz);
我在JUnit4和Im中創建了測試類,並依賴於JUnit4運行我的主程序。
你確定實現這些測試的類(你稱它們爲「testscripts」)每個都只有一個構造函數嗎? – Jesper 2012-07-26 07:28:18
是的,我確定。它是一個簡單的junit3測試。 公共類ScriptTest擴展了TestCase { } – jantox 2012-07-26 07:33:53
你能發佈多一點的代碼嗎?包括ScriptTest構造函數?還有套件的代碼。 – 2012-07-26 07:48:16