Running Tests in Parallel using TestNG


Quite often in automation testing, we want to reduce the test execution time to get the test results as fast as possible. When we run the selenium tests using as testNG suite, by default it runs the tests serially. But testNG provides an inherent support to run the tests in parallel.
Before starting with parallel execution, lets get a brief insight into TestNG.xml file (those familiar with testng.xml file can skip this section and move to next paragraph - "Running tests in parallel").
Testng.xml file eases test executions and grouping of multiple different tests from same or different classes. This XML file has different tags for suite, test, classes and/or packages with suite being the root tag.
Sample testNG.xml file-

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="ArtOfTestingTestSuite" verbose="1" >
  <test name="parallelExecutionTests" >
    <classes>
       <class name="testsInParallel" />
	   <class name="testsInParallel" />
    </classes>
  </test>
</suite>

On right clicking on the testng.xml file and running as TestNG suite, testNG will find the tests with @Test annotation in testsInParallel.java class and start test execution.

Running tests in parallel

In order to run the tests in parallel just add these two key value pairs in suite-

  • parallel="{methods/tests/classes}"
  • thread-count="{number of thread you want to run simultaneously}".
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="ArtOfTestingTestSuite" verbose="1" parallel="methods" thread-count="5">
  <test name="parallelExecutionTestsA" >
    <classes>
       <class name="testsInParallelA1" />
	   <class name="testsInParallelA2" />
    </classes>
  </test>
  <test name="parallelExecutionTestsB" >
    <classes>
       <class name="testsInParallelB1" />
	   <class name="testsInParallelB2" />
    </classes>
  </test>
</suite>

Let's see the purpose of the three options that can be set as value for the "parallel" parameter-

  • methods - method value for parallel attribute will run all the tests independently on separate threads(maximum available threads). In the above example all the test methods listed under all the four classes will run in parallel in maximum 5 threads.
  • tests - test value will run the methods specified in the test tag in the same thread. In the above example, all the test methods under the "parallelExecutionTestsA" test will run in same thread and all the test methods under the "parallelExecutionTestsB" will run in same thread. So, there will be two threads each running the test methods under parallelExecutionTestsA and parallelExecutionTestsB tests in parallel but independent of each other.
  • class - class value will run the all the test methods stated in a class in same thread and with each class's method running in different thread. So, there would be four thread running the tests in the four classes-testsInParallelA1,testsInParallelA2,testsInParallelB1,testsInParallelB2.