1

我有三个测试类: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 开始执行?parallel='both' 不是意味着类和方法同时执行吗?

编辑:我发现博客文章提到了这个问题。

4

2 回答 2

1

请参阅来自SUREFIRE-814 的评论(从 2012 年 1 月开始):

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

可以肯定的是,parallel=both 在此期间并未受到太多关注。我确认该问题会影响 Surefire 2.15,并且我计划对此发表评论并对票进行投票(一旦我收到我的注册确认)。我建议更多的人在票上投票。

于 2013-06-12T15:48:55.100 回答
0

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

于 2013-09-08T11:12:04.023 回答