Maven Surefire parallel ='both'的工作方式与'method's相同]]

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Maven Surefire parallel ='both'的工作方式与'method's相同]]相关的知识,希望对你有一定的参考价值。

我有三个测试类: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”值,但输出有些混乱。请看看并向我解释:

parallel ='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。

parallel ='方法'
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。

parallel ='both'
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开始执行?不等于=“两者”都意味着类和方法都同时执行吗?

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

我有三个测试类:FirstTest,SecondTest和ThirdTest。这是FirstTest类的样子:public class FirstTest @Test public void test()抛出InterruptedException System ....

答案

请参阅this comment中的SUREFIRE-814(从2012年1月开始:]:>

“两者”长期以来都未经测试,我没有理由不相信您看到此问题。

另一答案

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

以上是关于Maven Surefire parallel ='both'的工作方式与'method's相同]]的主要内容,如果未能解决你的问题,请参考以下文章

如何在 TestNG 中并行运行测试用例?

maven-surefire-plugin

Spring Boot 无法运行 maven-surefire-plugin ClassNotFoundException org.apache.maven.surefire.booter.Forke

无法编译 TestNG 测试套件(Maven+Surefire)

maven-surefire-plugin 忽略 pom.xml 中的插件顺序

maven-surefire-plugin,用于自动化测试和单元测试的