Maven Surefire parallel ='both'的工作方式与'方法'相同

时间:2013-06-10 10:25:42

标签: maven junit surefire

我有三个测试类:FirstTest,SecondTest和ThirdTest。这就是FirstTest类的样子:

public class FirstTest {

  @Test
  public void test() throws InterruptedException {
    System.out.println(this.getClass() + " " + new Date());
    Thread.sleep(10_000);
  }

  @Test
  public void test2() throws InterruptedException {
    System.out.println(this.getClass() + " " + new Date());
    Thread.sleep(10_000);
  }

  @Test
  public void test3() throws InterruptedException {
    System.out.println(this.getClass() + " " + new Date());
    Thread.sleep(10_000);
  }

  @Test
  public void test4() throws InterruptedException {
    System.out.println(this.getClass() + " " + new Date());
    Thread.sleep(15_000);
  }

}

SecondTest类是相同的(10s + 10s + 10s + 15s = 45s),ThirdTest包含10s + 10s + 10s测试方法。

我正在使用Maven Surefire插件进行以下配置:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.9</version>
    <configuration>
      <excludes>
        <exclude>${excludeTestPath}</exclude>
      </excludes>
      <parallel>both</parallel>
      <threadCount>20</threadCount>
    </configuration>
</plugin>

我遇到了解并行属性如何工作的问题。我已经尝试设置“both”,“methods”和“classes”值,但输出有些令人困惑。请看一下并向我解释一下:

平行= '类'

Concurrency config is parallel='classes', perCoreThreadCount=true, 
threadCount=20, useUnlimitedThreads=false
Running experiment.ThirdTest
class experiment.ThirdTest Mon Jun 10 12:00:38 CEST 2013
class experiment.ThirdTest Mon Jun 10 12:00:48 CEST 2013
class experiment.ThirdTest Mon Jun 10 12:00:58 CEST 2013
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 30.024 sec
Running experiment.SecondTest
class experiment.SecondTest Mon Jun 10 12:00:38 CEST 2013
class experiment.SecondTest Mon Jun 10 12:00:48 CEST 2013
class experiment.SecondTest Mon Jun 10 12:00:58 CEST 2013
class experiment.SecondTest Mon Jun 10 12:01:08 CEST 2013
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 45.054 sec
Running experiment.FirstTest
class experiment.FirstTest Mon Jun 10 12:00:38 CEST 2013
class experiment.FirstTest Mon Jun 10 12:00:48 CEST 2013
class experiment.FirstTest Mon Jun 10 12:00:58 CEST 2013
class experiment.FirstTest Mon Jun 10 12:01:08 CEST 2013
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 45.074 sec

Results :
Tests run: 11, Failures: 0, Errors: 0, Skipped: 0

[INFO] Total time: 52.319s
[INFO] Finished at: Mon Jun 10 12:01:48 CEST 2013
[INFO] Final Memory: 22M/354M

这是预期的结果,三个类并行执行,全部从12:00:38开始。构建应该需要10s + 10s + 10s + 15s,所以大约需要45s。

平行= '方法'

Concurrency config is parallel='methods', perCoreThreadCount=true,
threadCount=20, useUnlimitedThreads=false
Running experiment.FirstTest
class experiment.FirstTest Mon Jun 10 12:10:23 CEST 2013
class experiment.FirstTest Mon Jun 10 12:10:23 CEST 2013
class experiment.FirstTest Mon Jun 10 12:10:23 CEST 2013
class experiment.FirstTest Mon Jun 10 12:10:23 CEST 2013
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 45.142 sec
Running experiment.SecondTest
class experiment.SecondTest Mon Jun 10 12:10:38 CEST 2013
class experiment.SecondTest Mon Jun 10 12:10:38 CEST 2013
class experiment.SecondTest Mon Jun 10 12:10:38 CEST 2013
class experiment.SecondTest Mon Jun 10 12:10:38 CEST 2013
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 45.037 sec
Running experiment.ThirdTest
class experiment.ThirdTest Mon Jun 10 12:10:53 CEST 2013
class experiment.ThirdTest Mon Jun 10 12:10:53 CEST 2013
class experiment.ThirdTest Mon Jun 10 12:10:53 CEST 2013
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 30.009 sec

Results :
Tests run: 11, Failures: 0, Errors: 0, Skipped: 0

[INFO] Total time: 47.405s
[INFO] Finished at: Mon Jun 10 12:11:04 CEST 2013
[INFO] Final Memory: 22M/354M

这是预期的结果,所有方法都在单个类中并行执行。构建应该需要15s + 15s + 10s,所以大约需要45s。

平行= '两者'

Concurrency config is parallel='both', perCoreThreadCount=true, 
threadCount=20, useUnlimitedThreads=false
Running experiment.FirstTest
class experiment.FirstTest Mon Jun 10 12:18:40 CEST 2013
class experiment.FirstTest Mon Jun 10 12:18:40 CEST 2013
class experiment.FirstTest Mon Jun 10 12:18:40 CEST 2013
class experiment.FirstTest Mon Jun 10 12:18:40 CEST 2013
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 45.18 sec
Running experiment.SecondTest
class experiment.SecondTest Mon Jun 10 12:18:55 CEST 2013
class experiment.SecondTest Mon Jun 10 12:18:55 CEST 2013
class experiment.SecondTest Mon Jun 10 12:18:55 CEST 2013
class experiment.SecondTest Mon Jun 10 12:18:55 CEST 2013
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 45.031 sec
Running experiment.ThirdTest
class experiment.ThirdTest Mon Jun 10 12:19:10 CEST 2013
class experiment.ThirdTest Mon Jun 10 12:19:10 CEST 2013
class experiment.ThirdTest Mon Jun 10 12:19:10 CEST 2013
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 30.025 sec

Results :
Tests run: 11, Failures: 0, Errors: 0, Skipped: 0

[INFO] Total time: 47.521s
[INFO] Finished at: Mon Jun 10 12:19:20 CEST 2013
[INFO] Final Memory: 22M/354M

现在,这个对我来说很困惑。构建需要大约45秒,而它应该需要大约15秒(最长的测试方法)。为什么不是所有三个班级都在12:18:40开始执行?不并行='both'意味着类和方法同时执行吗?

编辑: 我发现blogpost提到了这个问题。

2 个答案:

答案 0 :(得分:1)

请参阅this comment {2012年1月} SUREFIRE-814

  

“both”在很长一段时间内基本未经测试,我没有理由不相信你会看到这个问题。

可以肯定的是,parallel =两者在过渡期间都没有受到太多关注。我确认该问题会影响Surefire 2.15,我计划对此进行评论并对机票进行投票(一旦我收到我的注册确认)。我建议更多人投票。

答案 1 :(得分:0)

此问题已在2.16中修复。 类似的问题也应该正常工作 https://jira.codehaus.org/browse/SUREFIRE-797