0

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

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

我尝试过的事情:

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

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

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

  4. 使用 JUnitCore.runClasses(JUnitTest.class) 代替创建核心实例并调用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);
    
4

2 回答 2

2

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

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

于 2016-04-25T06:53:59.097 回答
0

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

于 2016-04-25T06:03:49.050 回答