Search code examples
javamaven-2testngjava-6

TestNG Parallel Test Configuration


With TestNG on Java 6 here's what I'd like to do in my Maven project:

  1. Kick off a test method (testAbc) that can run multi-threaded using a queue-based DataProvider
  2. Kick off a test that relies on testAbc (testXyz) after all the threads from testAbc complete

I thought I had it configured properly, but I do not.

Currently I have it configured as such:

@Test ( singleThreaded = false )
public class AutomatedTest {

  @Test (
    alwaysRun = true,
    dataProviderClass = UseCaseProvider.class,
    dataProvider = "getUseCasesNoDependencies",
    skipFailedInvocations = false,
    threadPoolSize = 25
  )
  public void testAbc(UseCase useCase) {
    executeUseCase(useCase);
  }

  @Test (
    dependsOnMethods = {"testAbc"},
    dataProviderClass = UseCaseProvider.class,
    dataProvider = "getUseCasesDependencies",
    singleThreaded = true
  )
  public void testXyz(UseCase useCase) {
    executeUseCase(useCase);
  }
}

All of the tests in testAbc that are provided by the @DataProvider take a long time to run but can be run up to 25 simultaneously (there's a good few hundred of them). Everything in testXyz cannot be run in parallel as not only do all cases rely on testAbc, but the processing is just not friendly to thread pooling like that.

My Maven configuration is setup as follows:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <configuration>
        <parallel>methods</parallel>
        <threadCount>25</threadCount>
      </configuration>
    </plugin>
  </plugins>
</build>

When I run mvn -U test I don't see my tests actually running in parallel. Help!

$ mvn -version
Apache Maven 2.2.1 (r801777; 2009-08-06 15:16:01-0400)
Java version: 1.6.0_35
Java home: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
Default locale: en_US, platform encoding: MacRoman
OS name: "mac os x" version: "10.7.5" arch: "x86_64" Family: "mac"

Solution

  • What about adding threadPoolSize

    @Test ( singleThreaded = false, threadPoolSize = 5 )
    public class AutomatedTest {
    ...
    

    The threadPoolSize attribute allows you to specify how many threads should be allocated for this execution.

    EDIT

    Just noticed that you missed invocationCount. Please note that threadPoolSize is ignored if invocationCount is not specified.

    So please try leaving class @Test as it is and update testAbc

      @Test (
        alwaysRun = true,
        dataProviderClass = UseCaseProvider.class,
        dataProvider = "getUseCasesNoDependencies",
        skipFailedInvocations = false,
        threadPoolSize = 25,
        invocationCount = 25
      )
      public void testAbc(UseCase useCase) {
        executeUseCase(useCase);
      }