How to execute test Methods / classes in parallel in TestNG framework?

Refer: Execute multiple classes from testng xml

We can achieve this by running the classes or tests or methods with multiple threads.
If we want to run methods/classes in separate threads, we need to set ‘parallel’ attribute on the tag to ‘methods’ / ‘classes’
In suite tag change the “parallel” attribute value from “tests” to “classes” for example:
If you wish to execute
  1. tests in parallel then use = parallel=”tests”
  2. classes in parallel then use = parallel=”classes”
  3. Methods in parallel then use = parallel=”methods”
The parallel attribute on the <suite> 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-countallows 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:
Refer below example –
Class1: 

package package1;

import org.testng.annotations.Test;

public class Class1 {
@Test
public void Package1Class1Test1(){
System.out.println("Package1Class1Test1 is executed with Thread count: "+Thread.currentThread().getId());
}
@Test
public void Package1Class1Test2(){
System.out.println("Package1Class1Test2 is executed with Thread count: "+Thread.currentThread().getId());
}

}
Class2:

package package1;

import org.testng.annotations.Test;

public class Class2 {

@Test
public void Package1Class2Test1(){
System.out.println("Package1Class2Test1 is executed with Thread count: "+Thread.currentThread().getId());
}
@Test
public void Package1Class2Test2(){
System.out.println("Package1Class2Test1 is executed with Thread count: "+Thread.currentThread().getId());
}
}
Use below testng.xml.

<?xml version="1.0" encoding="UTF-8"?>
<suite name="example suite 1" verbose="1" parallel="methods" thread-count="2" >
<test name="Test suite 1" >
<classes>
<class name="package1.Class1"/>
<class name="package1.Class2"/>
</classes>
</test>
</suite>
In above testng.xml you can see parallel = “methods” which let us to decide what can we run in parallel, if it is a class or it is a method etc.

Thread-count = “2” let us decide in how many threads you wish to do execution in parallel.

Observe below console output

Console output shows the tests / methods execution is done in parallel. Just look at thread count.
Console Output:
[TestNG] Running:
  C:UsersPrakashworkspacesampleProjecttestng.xml
Package1Class1Test1 is executed with Thread count: 11
Package1Class1Test2 is executed with Thread count: 12
Package1Class2Test1 is executed with Thread count: 12
Package1Class2Test1 is executed with Thread count: 11
===============================================
example suite 1
Total tests run: 4, Failures: 0, Skips: 0
===============================================