任何人都遇到了这个错误测试类只能有一个构造
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