为什么JUnitCore只返回第一个结果?

时间:2016-04-24 22:00:25

标签: java junit eclipse-jdt mutation-testing

我正在使用JODit 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
}

上面的代码适用于第一个测试,但之后的每个测试都会返回相同的结果。为什么即使进行了不同的突变也没有产生新结果?

我尝试过的事情:

  1. 测试在每次循环迭代时都会产生不同的突变。

  2. 测试在运行测试之前编译新代码。

  3. 将for循环的内部作为线程运行,等待该线程完成,然后运行下一个测试。

  4. 使用JUnitCore.runClasses(JUnitTest.class)代替创建 core 的实例并调用 core.run(JUnitTest.class)

    JUnitCore core = new JUnitCore();
    Result result = core.run(JUnitTest.class);
    
  5. 将JUnitCore(org.junit)代码替换为TestRunner(junit.textui),这给了我同样的问题:

    TestSuite suite= new TestSuite();
    suite.addTestSuite(JUnitTest.class);
    TestResult result = TestRunner.run(suite);
    

2 个答案:

答案 0 :(得分:2)

您需要将突变体插入JVM - 尽管您正在编译修改后的文件,但JVM只会看到第一个加载的版本。

有多种方法可以做到这一点,从为每个突变体启动一个新的JVM到使用仪器API。

答案 1 :(得分:0)

为什么要从 i = 0 i = 10 运行循环, 如果使用这个循环,那么你在代码中使用 i 的值。我认为这不使用 i 的值,每次都会产生相同的结果。

相关问题