Software Testing

TestNG parallel attribute

TestNG parallel attribute

In TestNG you can perform parallel execution by two ways. One with testng.xml file and you can configure a test method to run in multiple threads. Let us look at simple example for parallel execution of Test Methods using ‘parallel’ attribute on the tag with ‘method’.

Let’s create a class with two test methods and try to execute in different threads.

package com.testingdocs.demo;
import org.testng.annotations.Test;

public class ParallelTest {

@Test
public void testCaseOne() {
//Print Id of the thread on using which test method got executed
System.out.println("Test Case One with Thread Id:- "
+ Thread.currentThread().getId());
}

@Test
public void testCaseTwo() {
////Print Id of the thread on using which test method got executed
System.out.println("Test Case two with Thread Id:- "
+ Thread.currentThread().getId());
}
}

testng.xml file

The testng.xml file, with attributes ‘parallel’ and ‘thread-count’ at suite level. To execute test methods in parallel, you can provide ‘methods’ and ‘thread-count’ attribute is to used to -pass the number of maximum threads to be created.

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parallel test suite" parallel="methods" thread-count="2">
  <test name="Parallel Demo Test">
    <classes>
      <class name="com.testingdocs.demo.ParallelTest"/>
    </classes>
  </test>
</suite>