08. Parallelism and time outs - senthilpazhani/SeleniumComplete GitHub Wiki

You can instruct TestNG to run your tests in separate threads in various ways.

Parallel suites

This is useful if you are running several suite files (e.g. "java org.testng.TestNG testng1.xml testng2.xml") and you want each of these suites to be run in a separate thread. You can use the following command line flag to specify the size of a thread pool:

java org.testng.TestNG -suitethreadpoolsize 3 testng1.xml testng2.xml testng3.xml

The corresponding ant task name is suitethreadpoolsize.

Parallel tests, classes and methods

The parallel attribute on the tag can take one of following values:

 <suite name="My suite" parallel="methods" thread-count="5">
 <suite name="My suite" parallel="tests" thread-count="5">
 <suite name="My suite" parallel="classes" thread-count="5">
 <suite name="My suite" parallel="instances" thread-count="5">
  • parallel="methods": TestNG will run all your test methods in separate threads. Dependent methods will also run in separate threads but they will respect the order that you specified.
  • parallel="tests": TestNG will run all the methods in the same <test> tag in the same thread, but each <test> tag will be in a separate thread. This allows you to group all your classes that are not thread safe in the same <test> and guarantee they will all run in the same thread while taking advantage of TestNG using as many threads as possible to run your tests.
  • parallel="classes": TestNG will run all the methods in the same class in the same thread, but each class will be run in a separate thread.
  • parallel="instances": TestNG will run all the methods in the same instance in the same thread, but two methods on two different instances will be running in different threads.

Additionally, the attribute thread-count allows you to specify how many threads should be allocated for this execution.

Note: the @Test attribute timeOut works in both parallel and non-parallel mode.

You can also specify that a @Test method should be invoked from different threads. You can use the attribute threadPoolSize to achieve this result:

 @Test(threadPoolSize = 3, invocationCount = 10,  timeOut = 10000)
 public void testServer() {

In this example, the function testServer will be invoked ten times from three different threads. Additionally, a time-out of ten seconds guarantees that none of the threads will block on this thread forever.

   @Test(timeOut = 300)
   public void testMethod2() throws InterruptedException {
      System.out.println("testMethod2");
      Thread.sleep(2000);
   }
⚠️ **GitHub.com Fallback** ⚠️