简体   繁体   English

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

[英]Why does JUnitCore only return the first results?

I am using JUnit 4 with Eclipse JDT to create automated Mutant testing. 我将JUnit 4与Eclipse JDT结合使用来创建自动化的Mutant测试。

Here is the general overview of my code structure: 这是我的代码结构的一般概述:

//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
}

My code above works for the first test, but every test after that always returns the same results. 我上面的代码适用于第一个测试,但是此后的每个测试始终返回相同的结果。 Why are new results not generated even though different mutations are made? 为什么即使进行了不同的突变也不会产生新的结果?

Things I have tried: 我尝试过的事情:

  1. Testing that different mutations are made at every loop iteration. 测试每个循环迭代中是否进行了不同的突变。

  2. Testing that the new code is compiled before the test is run. 在测试运行之前测试是否已编译新代码。

  3. Running the internals of the for loop as a thread, wait for that thread to finish, then run the next test. 将for循环的内部作为线程运行,等待该线程完成,然后运行下一个测试。

  4. Using JUnitCore.runClasses(JUnitTest.class) in lieu of creating an instance of core and calling core.run(JUnitTest.class) : 使用JUnitCore.runClasses(JUnitTest.class)代替创建core实例并调用core.run(JUnitTest.class)

     JUnitCore core = new JUnitCore(); Result result = core.run(JUnitTest.class); 
  5. Substituting JUnitCore (org.junit) code for TestRunner (junit.textui), which gave me the same problem: 用JUnitCore(org.junit)代码代替TestRunner(junit.textui),这给了我同样的问题:

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

You need to insert the mutant into the JVM - although you are compiling the modified file the JVM will only see the first loaded version. 您需要将变体插入JVM-尽管您正在编译修改后的文件,但JVM仅会看到第一个加载的版本。

There are various ways to do this, from launching a new JVM for each mutant through to using the instrumentation API. 有多种方法可以执行此操作,从为每个变体启动新的JVM到使用工具API。

Why are you running the loop from i=0 to i=10 , and if using this loop than where are you using the value of i inside the code. 为什么要运行从i = 0i = 10的循环,如果使用此循环,那么为什么要在代码内部使用i的值。 I think this not using the value of i resulting you the same result every time. 我认为这没有使用i的值,每次都导致相同的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM