TestNG - Parallel Execution

TestNG allows to run tests parallelly or in separate threads in following ways:

  • Parallel suites: If you are running several suite files (e.g. testng1.xml testng2.xml"), and you want each of these suites to be run in a separate thread. 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
  • Parallel tests, classes and methods: Use parallel attribute on the <suite> tag respectively (for methods,test,classes, instances).

          <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 testing is used heavily with Selenium because of the importance of cross-browser testing. With so many browsers in market today with a different version, create a browser matrix and run the tests parallelly. This will save us lot of time and other resources.

Advantages and Disadvantages

Following are some of the advantages of parallel testing using TestNG:

  • It reduces time

  • Allows multi-threaded tests

Following are some of the disadvantages of parallel testing using TestNG

  • Fails on dependent modules - Most of the times the tests are inter-dependent, hence failing chances are more.

  • Program flow sequence - The tester should be well aware of the program flow to create parallel testing modules.

Create Test Case Class

Let us see an example to run test methods parallelly. Create a java class, say, TestParallel.java in /work/testng/src.

  import org.testng.annotations.Test;

  public class TestParallel {
  	   public void method1() {
  	      System.out.println("Inside method1()");
  	      //Assert.assertEquals(message, messageUtil.printMessage());
  	public void method2() {
  	      System.out.println("Inside method2()");
  	      //Assert.assertEquals(message, messageUtil.printMessage());

The preceding test class contains two test methods which will run in separate threads.

Create testng.xml

Create testng.xml in /work/testng/src to execute test case(s).

  <?xml version = "1.0" encoding = "UTF-8"?>
  <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
  <suite name = "Parallel Testing Suite">
     <test name = "Parallel Tests" parallel = "methods">
           <class name = "TestParallel" />

Compile the TestParallel class using javac.

/work/testng/src$ javac TestParallel.java

Now, run testng.xml.

/work/testng/src$ java org.testng.TestNG testng.xml

Verify the output.

Inside method1()
Inside method2()

Parallel Testing Suite
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
Kickstart Your Career

Get certified by completing the course

Get Started