
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to Limiting the Number of Parallel Tests with ThreadCount in TestNG?
TestNG supports multi?threading i.e. a @Test methods can be invoked parallelly. A test or multiple test methods can be invoked from multiple threads. Therefore, multiVthread is useful if @Test methods need to be run asynchronously in parallel.
Multi?threading can be achieved by using keyword ? thread?count =<integer> at Testng.xml. Thread count is basically number of instances running to execute multiple tests simultaneously or parallelly. The attribute thread?count allows the user to specify how many threads should be run for this execution.
In this example, 5 @Test method will execute in parallel from 5 threads.
In this article, we will illustrate how to achieve multithreading.
Approach/Algorithm to Solve this Problem
Step 1: Create a TestNG class ? NewTestngClass.
Step 2: Write 5 @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?ount and parallel keyword.
Step 4: Now, run the testNG.xml or directly testNG class in IDE or compile and run it using command line.
In the output, user can see total 5 threads are running in parallel ? ID 18 to 22.
Example
The following code for 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 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 ===============================================