There are several ways to run Selenium tests in parallel using TestNG. TestNG allows you to execute methods, classes, or test tags concurrently by configuring the parallel attribute in the TestNG suite file.
Below is an example testng.xml suite file that demonstrates how to execute test classes in parallel:
testng.xml
<suite name=”ParallelSuite” parallel=”methods” thread-count=”2″>
<test name=”ParallelTests”>
<classes>
<class name=”TestClass” />
</classes>
</test>
</suite>
By setting the parallel attribute in the TestNG suite file, we can control how tests are executed in parallel:
- Setting
parallel="methods"runs test methods in parallel within the same class. - Setting
parallel="tests"allows multiple<test>blocks to run simultaneously. - Setting
parallel="classes"enables parallel execution of different test classes.
Below is an example TestClass demonstrating parallel method execution:
import org.testng.annotations.Test;
public class TestClass {
@Test
public void test1() {
System.out.println(“Test 1 – ” + Thread.currentThread().getId());
}
@Test
public void test2() {
System.out.println("Test 2 - " + Thread.currentThread().getId());
}
}