我正在使用 JUnit 4 和 Eclipse JDT 来创建自动化的 Mutant 测试。
这是我的代码结构的一般概述:
//Basic for loop
for(int i = 0; i < 10; i++) {
//Read in source code from a Java file
...(works)
//Change a line using JDT and save code to a new Java file
...(works)
//Compile new Java file (this also works)
try {
Process compile = Runtime.getRuntime().exec("javac -cp \"src/*\" " + path + "*.java");
compile.waitFor();
} catch(IOException ex) { /*...*/ }
catch(InterruptedException ex) { /*...*/ }
//Run JUnit Tests (this works the first time it is called)
JUnitCore core = new JUnitCore();
Result result = core.run(JUnitTest.class); //This class contains my JUnit Tests
}
我上面的代码适用于第一个测试,但之后的每个测试总是返回相同的结果。为什么即使进行了不同的突变,也没有产生新的结果?
我尝试过的事情:
测试在每次循环迭代中都会产生不同的突变。
在运行测试之前测试新代码是否已编译。
将 for 循环的内部作为线程运行,等待该线程完成,然后运行下一个测试。
使用 JUnitCore.runClasses(JUnitTest.class) 代替创建核心实例并调用core.run(JUnitTest.class):
JUnitCore core = new JUnitCore(); Result result = core.run(JUnitTest.class);
用 JUnitCore (org.junit) 代码替换 TestRunner (junit.textui),这给了我同样的问题:
TestSuite suite= new TestSuite(); suite.addTestSuite(JUnitTest.class); TestResult result = TestRunner.run(suite);