Parallel Execution

TestNG allows parallel execution by which you can run tests simultaneously on different threads. You can run your tests in following ways by distributing to different threads.

  • Parallel suites
  • Parallel tests
  • Parallel classes
  • Parallel methods
  • Parallel instances

Lets understand each of these parallel execution method in more detail. For this we will create two java class files ExampleTest1.java and ExampleTest2.java

ExampleTest1.java

Java
Copy
package org.example;

import org.testng.annotations.Test;
public class ExampleTest1 {
    @Test
    public void ExampleTest1Method1()
    {
        String threadName = Thread.currentThread().getName();
        System.out.println(String.format("Thread = " + threadName +  ", testInstance = [" + this.getClass().getName()  + " - " + "Method1 - " + this + "]"));
    }
    @Test
    public void ExampleTest1Method2()
    {
        String threadName = Thread.currentThread().getName();
        System.out.println(String.format("Thread = " + threadName +  ", testInstance = [" + this.getClass().getName()  + " - " + "Method2 - " + this + "]"));
    }
}

ExampleTest2.java

Java
Copy
package org.example;

import org.testng.annotations.Test;
public class ExampleTest2 {
    @Test
    public void ExampleTest2Method1()
    {
        String threadName = Thread.currentThread().getName();
        System.out.println(String.format("Thread = " + threadName +  ", testInstance = [" + this.getClass().getName()  + " - " + "Method1 - " + this + "]"));
    }
    @Test
    public void ExampleTest2Method2()
    {
        String threadName = Thread.currentThread().getName();
        System.out.println(String.format("Thread = " + threadName +  ", testInstance = [" + this.getClass().getName()  + " - " + "Method2 - " + this + "]"));
    }
}

Parallel suites


This is useful when you want to run multiple suites simultaneously on separate thread. Let's say you have two suites ie Feature1.xml and Feature2.xml. Each suite will run on separate thread.


Create Feature1.xml

XML
Copy
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Feature1">
    <test name="Feature1">
        <classes>
            <class name="org.example.ExampleTest1"/>
    </test>
</suite>

Create Feature2.xml

XML
Copy
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Feature2">
    <test name="Feature2">
        <classes>
            <class name="org.example.ExampleTest2"/>
    </test>
</suite>



To run multiple suites, you can use the following command. You can also specify the size of the thread pool.

java org.testng.TestNG -suitethreadpoolsize 3 Feature1.xml Feature2.xml

Parallel tests


In the parallel tests, TestNG will run all the methods under the same test tag in then same thread but each test will get a separate thread. The benefit of this is that you can group all your not-thread safe classes in the same test tag and it will be guaranteed that they all will run in the same thread.

Create a new suite file testng.xml as below.

XML
Copy
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parallel Tests" parallel="tests" thread-count="5">
    <test name="Parallel Test 1">
        <classes>
            <class name="org.example.ExampleTest1"/>
        </classes>
    </test>
    <test name="Parallel Test 2">
        <classes>
            <class name="org.example.ExampleTest2"/>
        </classes>
    </test>
</suite>


In the above xml file we have created two tests, each having one class file. Also we have added attribute value for parallels as tests and thread-count to 5. Now run the xml file and see if you are able to see results as below.

Thread = TestNG-tests-2, testInstance = [org.example.ExampleTest2 - Method1 - org.example.ExampleTest2@4445629]
Thread = TestNG-tests-1, testInstance = [org.example.ExampleTest1 - Method1 - org.example.ExampleTest1@1df82230]
Thread = TestNG-tests-1, testInstance = [org.example.ExampleTest1 - Method2 - org.example.ExampleTest1@1df82230]
Thread = TestNG-tests-2, testInstance = [org.example.ExampleTest2 - Method2 - org.example.ExampleTest2@4445629]
===============================================
Parallel Tests
Total tests run: 4, Passes: 4, Failures: 0, Skips: 0
===============================================


In the above result you can see each test methods under the tests are run on same thread and both tests were run on different thread ie 1 an 2.

Parallel classes


When you are running parallel classes, TestNG will run all the test methods in the a class in the same thread, but each class will be run in a separate thread. For this you have to change the suite attribute value of parallel to classes. Let's understand this by following example.

XML
Copy
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parallel Classes" parallel="classes" thread-count="5">
    <test name="Parallel Classes">
        <classes>
            <class name="org.example.ExampleTest1"/>
            <class name="org.example.ExampleTest2"/>
        </classes>
    </test>
</suite>


Create a new xml suite file testng.xml file as above and see if you are able to see below results. Here TestNG will run each methods from each class in same thread. If you look at the log below you can see the methods of class ExampleTest1 was run on thread 1 and methods of class ExampleTest2 was run on thread 2.


Thread = TestNG-test=Parallel Classes-1, testInstance = [org.example.ExampleTest1 - Method1 - org.example.ExampleTest1@78b729e6]
Thread = TestNG-test=Parallel Classes-2, testInstance = [org.example.ExampleTest2 - Method1 - org.example.ExampleTest2@6293abcc]
Thread = TestNG-test=Parallel Classes-1, testInstance = [org.example.ExampleTest1 - Method2 - org.example.ExampleTest1@78b729e6]
Thread = TestNG-test=Parallel Classes-2, testInstance = [org.example.ExampleTest2 - Method2 - org.example.ExampleTest2@6293abcc]
===============================================
Parallel Classes
Total tests run: 4, Passes: 4, Failures: 0, Skips: 0
===============================================

Parallel methods


When you specify Parallel methods, TestNG will run all your test methods in separate threads. Here test methods will be from any classes. Any test methods which has any dependent methods will also run in separate threads but they will run in the order that you specified. For this you have to change the suite attribute value of parallel to methods. Let's create a new suite xml file prallelMethods.xml or by changing the value of parallel attribute to methods in the previous file and run the xml file to see the result. The updated xml file is as follows:

XML
Copy
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parallel Methods" parallel="methods" thread-count="5">
    <test name="Parallel Methods">
        <classes>
            <class name="org.example.ExampleTest1"/>
            <class name="org.example.ExampleTest2"/>
        </classes>
    </test>
</suite>


Check if you are able to see below result. If you look at below log, you can see all the methods are run on different thread ie 1, 2, 3 and 4.


Thread = TestNG-test=Parallel Methods-2, testInstance = [org.example.ExampleTest1 - Method2 - org.example.ExampleTest1@1d76aeea]
Thread = TestNG-test=Parallel Methods-3, testInstance = [org.example.ExampleTest2 - Method1 - org.example.ExampleTest2@22635ba0]
Thread = TestNG-test=Parallel Methods-4, testInstance = [org.example.ExampleTest2 - Method2 - org.example.ExampleTest2@22635ba0]
Thread = TestNG-test=Parallel Methods-1, testInstance = [org.example.ExampleTest1 - Method1 - org.example.ExampleTest1@1d76aeea]
===============================================
Parallel Methods
Total tests run: 4, Passes: 4, Failures: 0, Skips: 0
===============================================

Parallel instances


TestNG will run all the methods belong to same instance in the same thread, but each instance will have different thread to run its methods.