What exactly does a Thread count do in TestNG?

TestNGRest AssuredDynamic Programming

TestNG supports multithreading, i.e., a @Test methods can be invoked in parallel. A test or multiple test methods can be invoked from multiple threads. Therefore, multithreading is useful if @Test methods need to be run asynchronously in parallel.

Multithreading can be achieved by using the keyword "thread-count=<integer>" at Testng.xml. Thread count is basically a number of instances running to execute multiple tests simultaneously or in parallel. The attribute thread-count allows the user to specify how many threads should be run for this execution.

In this article, we will illustrate how to achieve multithreading.

Approach/Algorithm to solve this problem

In this example, five @Test methods will execute in parallel from five different threads.

  • Step 1 − Create a TestNG class, NewTestngClass.

  • Step 2 − Write five @Test methods in the class NewTestngClass, as shown in programming code section.

  • Step 3 − Now create the testNG.xml as given below to run the TestNG classes. Add thread-count and parallel keyword.

  • Step 4 − Finally, run the testNG.xml or directly testNG class in IDE or compile and run it using command line.

In the output, the user can see a total of five threads running in parallel – ID 18 to 22.

Example

Use the following code for the common TestNG class, NewTestngClass

src/ NewTestngClass.java

import org.testng.ITestContext;
import org.testng.annotations.*;
public class NewTestngClass {
   @Test()
   public void testcase1(ITestContext testContext){
      System.out.println("Thread ID: "+Thread.currentThread().getId());
      int currentCount = testContext.getAllTestMethods()[0].getCurrentInvocationCount();
      System.out.println("Executing count: " + currentCount);
   }
   @Test()
   public void testcase2(ITestContext testContext){
      System.out.println("Thread ID:"+Thread.currentThread().getId());
      int currentCount = testContext.getAllTestMethods()[1].getCurrentInvocationCount();
      System.out.println("Executing count: " + currentCount);
   }
   @Test()
   public void testcase3(ITestContext testContext){
      System.out.println("Thread ID:"+Thread.currentThread().getId());
      int currentCount = testContext.getAllTestMethods()[2].getCurrentInvocationCount();
      System.out.println("Executing count: " + currentCount);
   }
   @Test()
   public void testcase4(ITestContext testContext){
      System.out.println("Thread ID: "+Thread.currentThread().getId());
      int currentCount = testContext.getAllTestMethods()[3].getCurrentInvocationCount();
      System.out.println("Executing count: " + currentCount);
   }
   @Test()
   public void testcase5(ITestContext testContext){
      System.out.println("Thread ID: "+Thread.currentThread().getId());
      int currentCount = testContext.getAllTestMethods()[4].getCurrentInvocationCount();
      System.out.println("Executing count: " + currentCount);
   }
}

testng.xml

This is a configuration file that is used to organize and run the TestNG test cases. It is very handy when limited tests are needed to execute rather than the full suite.

<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name = "Suite1" thread-count="5" parallel="methods">
   <test name = "test1">
      <classes>
         <class name = "NewTestngClass"/>
      </classes>
   </test>
</suite>

Output

Thread ID: 18
Thread ID: 22
Thread ID: 19
Executing count: 0
Thread ID: 20
Executing count: 0
Thread ID: 21
Executing count: 0
Executing count: 0
Executing count: 0
===============================================
Suite1
Total tests run: 5, Passes: 5, Failures: 0, Skips: 0
===============================================
raja
Updated on 12-Jan-2022 13:34:13

Advertisements