【问题标题】:Why does JUnitCore only return the first results?为什么 JUnitCore 只返回第一个结果?
【发布时间】:2016-08-18 04:24:42
【问题描述】:

我正在使用 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 的实例并调用 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);
    

【问题讨论】:

    标签: java junit eclipse-jdt mutation-testing


    【解决方案1】:

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

    【讨论】:

    • 循环是更高级代码的总结。实际上,我正在搜索每个中缀表达式的代码并对其进行变异。循环的数量可能会有所不同,因此为简单起见,我将问题宣传为简单地运行 10 次(尽管即使两次也不起作用)。
    【解决方案2】:

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

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-06
    • 1970-01-01
    • 2014-04-17
    • 2016-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多