
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
Invoke Test Method Using Multiple Threads in TestNG
TestNG supports multi-threading, i.e., a @Test method can be invoked multiple times in parallel. A test method should be invoked from multiple threads so that multiple invocation is also required. If we want to run a single @Test at multi-thread, it is of no use. Therefore, multi-thread is useful if a @Test method needs to be run multiple times asynchronously.
Multi-threading can be achieved by using the keyword threadPoolSize = at @Test. However, to invoke a method multiple times, another keyword invocationCount = is required. Combining these two keywords, we can achieve multi-threading. For example,
@Test(threadPoolSize=5, invocationCount = 10)
In this example, the @Test method will execute for a total of 10 times from 5 threads. Note that the count of total execution is 10 from 5 different threads. It doesn't mean that @Test will run a total of 50 times or 10 times in each thread. It simply means, 5 threads will execute the method a total of 10 times.
In this article, we will illustrate how to achieve multithreading in TestNG.
Approach/Algorithm to solve this problem −
Step 1 − Create a TestNG class called NewTestngClass.
Step 2 − Write a @Test method in the class, as shown in the program code section. Add threadPoolSize and invocationCount
Step 3 − Now create the testNG.xml to run the TestNG classes.
Step 4 − Run the testNG.xml or run the testNG class directly in IDE or compile and run it using command line.
In the output, you can notice that there are 5 threads are running in parallel (Thread ID 12 to 16) and the method runs for a total of 10 times.
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(threadPoolSize = 5, invocationCount = 10) 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); } }
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"> <test name = "test1"> <classes> <class name = "NewTestngClass"/> </classes> </test> </suite>
Output
Thread ID: 12 Thread ID: 13 Thread ID: 16 Thread ID: 14 Executing count: 0 Thread ID: 15 Executing count: 0 Executing count: 0 Executing count: 0 Executing count: 0 Thread ID: 15 Executing count: 5 Thread ID: 12 Executing count: 6 Thread ID: 13 Executing count: 7 Thread ID: 14 Executing count: 8 Thread ID: 15 Executing count: 9 =============================================== Suite1 Total tests run: 10, Passes: 10, Failures: 0, Skips: 0 ===============================================================