2015-10-07 93 views
10

我正在Selenium网格上使用Surefire插件执行测试运行Selenium测试。 在我的测试细分方面,我有几个班级,其中一些班级有一个测试,并有一个以上的测试。JUnit和Surefire并行测试 - ForkCount和ThreadCount

所以在我的网格上我有30个chrome web驱动程序,我想在所有类中并行执行所有测试。

我读过如何使用parallel参数,我已经设置为做到这一点:

  <plugin> 
       <artifactId>maven-surefire-plugin</artifactId> 
       <version>2.17</version> 
       <configuration> 
        <includes> 
         <include>${testSuite}</include> 
        </includes> 
        <parallel>all</parallel> 
        <useSystemClassLoader>false</useSystemClassLoader> 
        <perCoreThreadCount>false</perCoreThreadCount> 
        <threadCount>20</threadCount> 
        <browser>${browser_type}</browser> 
       </configuration> 
      </plugin> 

然而,这似乎不填写的所有Chrome网络驱动器我有可用。

如果我再使用forkCount,如:

<forkCount>20</forkCount> 
<reuseForks>true</reuseForks> 

然后,当测试执行第一次启动时,所有的网络驱动程序,但是它充满很快开始下降和行为一次一个。

所以我的问题:

  • 有forkCount和THREADCOUNT
  • 之间的关系有什么额外的,我需要做才能真正获得并行运行这个?

谢谢。

+0

是否使用@NotThreadSafe在你的测试?你需要的一切在这里https://maven.apache.org/surefire/maven-surefire-plugin/examples/fork-options-and-parallel-execution.html –

+0

没有 - 没有使用@NotThreadSafe – userMod2

+0

硒脚本旨在平行运行?否则所有线程操作都将在单个浏览器中发生。 – parishodak

回答

3

你必须提供明确的JUnit测试提供商:

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-surefire-plugin</artifactId> 
    <version>2.18.1</version> 
    <dependencies> 
     <dependency> 
      <groupId>org.apache.maven.surefire</groupId> 
      <artifactId>surefire-junit47</artifactId> 
      <version>2.18.1</version> 
     </dependency> 
    </dependencies> 
    <configuration> 
     <parallel>all</parallel> 
     <useUnlimitedThreads>true</useUnlimitedThreads> 
     <useSystemClassLoader>false</useSystemClassLoader> 
     <includes> 
      <include>${testSuite}</include> 
     </includes> 
     <systemPropertyVariables> 
      <browser>${browser_type}</browser> 
     </systemPropertyVariables> 
    </configuration> 
</plugin> 

你应该使用JUnit 4.7+旧版本不并行测试正常工作。

如果您的测试不影响JVM运行时(通常情况并非如此),那么您也可以忽略与叉相关的参数。

或者将您的测试迁移到TestNG--它是更优雅的框架,它与平行测试更好地工作,然后JUnit(imo)。

+0

如果您使用junit 4.8+并在配置中使用参数,则会自动选择surefire-junit47:http://maven.apache.org/surefire/maven-surefire-plugin/examples/junit.html#Using_JUnit_Categories – Ardesco

+0

@ Ardesco - 真/假。真的,因为你是对的。错误,因为OP不使用组。然而明确的声明保证将使用正确的测试提供者。 – ursa

+0

对插件使用dependecnies没有区别。 (我正在使用jUnit 4.11) – userMod2

0

并行运行测试有很多配置。

根据文档:

forkCount

参数forkCount定义JVM的最大进程数神火将同时产卵执行测试。它支持与maven-core中的-T相同的语法:如果用C终止该值,则该值将乘以系统中可用CPU内核的数量。例如,四核系统上的forkCount=2.5C将导致执行测试的最多十个并发JVM进程分叉。

...

默认设置为forkCount=1/reuseForks=true,这意味着神火创建一个新的JVM进程中的一个Maven的模块来执行所有测试。

THREADCOUNT

当使用reuseForks=trueforkCount值大于一,测试类都交给叉形过程一个接一个。因此,parallel=classes不会改变任何东西。但是,您可以使用parallel=methods:在forkCount并发进程中执行类,然后每个进程可以使用threadCount线程并行执行一个类的方法。

就我所知,threadCount就像每个叉子的子线程。

您可以调整这些PARAMS提高你的考试成绩,比如,你可以有:

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-surefire-plugin</artifactId> 
    <version>2.17</version> 
    <configuration> 
     <includes> 
       <include>${testSuite}</include> 
     </includes> 
     <parallel>all</parallel> 
     <useSystemClassLoader>false</useSystemClassLoader> 
     <perCoreThreadCount>false</perCoreThreadCount> 
     <forkCount>2.0C</forkCount> 
     <reuseForks>true</reuseForks> 
     <threadCount>20</threadCount> 
     <browser>${browser_type}</browser> 
    </configuration> 
</plugin> 

你可以在其网站的详细信息:

https://maven.apache.org/surefire/maven-surefire-plugin/examples/fork-options-and-parallel-execution.html

+0

我已经尝试过这些,但是我得到的问题是我所有的chrome驱动程序都没有被填充,即使我正在对它进行足够多的测试。 – userMod2

+0

@ userMod2似乎是您的测试用例设置的问题,而不是您想要同时运行它们的方式。例如,你可以删除所有的并行性,并检查你的驱动程序是否已满,如果没有,那么你有另一个问题。据我所知,你可能在'@ Before'或'@ BeforeClass'注释方法中有问题,你是否在那里加载你的驱动程序? –

+0

我删除了任何并行配置,它只运行在一个Web驱动程序中,所以我的配置正在被使用。 另外,在我的代码中没有任何Before或@BeforeClass方法 – userMod2