There are a lot of times that we want to run multiple test cases at the same time. This is also called execution in parallel. This can reduce execution time tremendously, because all test cases executed simultaneously. TestNG just provide the ways for us to achieve this.
Execute TestNG Test In Parallel
<suite name="ParallelTestSuite" parallel="false" thread-count="2"> <test name="Test1"> <classes> <class name="com.dev2qa.example.testng.parallel.TestNGParallelExample"/> </classes> </test> </suite>
From above xml file, we can see that xml element suite has a parallel attribute, we can set it’s value to make test case run parallel in different level.
You can click here to goto TestNG document page to learn all values of parallel parameter. From the official document, we can see the parallel attribute’s value can be false, methods, tests, classes and instances. We will show you examples for them later.
Example 1 : Execute Test Method In Single Thread.
- Create a java project in Eclipse and add TestNG Eclipse plugin in it. You can read TestNG Eclipse Plugin for detail introduction.
- Right click the project, click ” New —> Package ” in popup menu.
- Input package name
com.dev2qa.example.testng.parallel
as below picture, click Finish button to create it.
- Right click java project, click ” New —> Others “, Input TestNG in popup dialog, select “TestNG class” icon. Click Next.
- In the next dialog, input class name, and xml suite file testng1.xml include it’s file path as below picture. Please note the xml suite file path is same as the class.
- Click Finish button, you can see that TestNGParallelExample1.java file and testng1.xml suite file in left project list panel.
- Create TestNGParallelExample2.java, testng2.xml and main-suite.xml in the folder also.
- Now let’s see the content of above files. You can see the java code comments for detail explanation.
- TestNGParallelExample1.java
public class TestNGParallelExample1 { /* Open a Firefox browser.*/ @Test public void testFirefox() throws InterruptedException { WebDriver driver = new FirefoxDriver(); driver.get("http://www.bing.com"); Thread.sleep(10000); driver.close(); } /* Open a Chrome browser.*/ @Test public void testChrome() throws InterruptedException { String chromeDriverExeFilePath = "C:\\Workspace\\dev2qa.com\\Lib\\chromedriver_win32\\chromedriver.exe"; // Assign ChromeDriver executable file to system property "webdriver.chrome.driver" System.setProperty("webdriver.chrome.driver", chromeDriverExeFilePath); // Create the Chrome Web Browser. WebDriver driver = new ChromeDriver(); Thread.sleep(10000); driver.close(); } /* Print out current executor thread info after each Test method run.*/ @AfterMethod public void afterMethod(ITestResult result) { long threadId = Thread.currentThread().getId(); TestNGParallelUtil.printTestInfo(result, threadId, "After run"); } }
- TestNGParallelExample2.java
public class TestNGParallelExample2 { /* Open an Internet Explorer browser.*/ @Test public void testIE() throws InterruptedException { String ieDriverFilePath = "C:\\Workspace\\dev2qa.com\\Lib\\IEDriverServer_x64_3.4.0\\IEDriverServer.exe"; //Specify the IEDriver executable file to system property. System.setProperty("webdriver.ie.driver", ieDriverFilePath); //Initiate web browser WebDriver driver = new InternetExplorerDriver(); Thread.sleep(10000); driver.close(); } /* Print out current executor thread info after each Test method run.*/ @AfterMethod public void afterMethod(ITestResult result) { long threadId = Thread.currentThread().getId(); TestNGParallelUtil.printTestInfo(result, threadId, "After run"); } }
- TestNGParallelUtil.java
public class TestNGParallelUtil { /* Print out the running test related info. */ public static void printTestInfo(ITestResult result, long threadId, String prefix) { String testName = result.getTestContext().getName(); String testClasName = result.getTestClass().getName(); String testMethod = result.getMethod().getMethodName(); System.out.println(prefix + " test method : " + testMethod + " , thread id : " + threadId + " , testName : " + testName + " , testClasName : " + testClasName); } }
- testng1.xml
There has two test defined in testng1.xml.
TestExample1 : Run Test method in classTestNGParallelExample1
.
TestExample2 : Run Test method in classTestNGParallelExample2
The suite xml element’s parallel attribute is false which means all test will run in a single thread.<?xml version="1.0" encoding="UTF-8"?> <suite name="ParallelTestSuite" parallel="false" thread-count="3"> <test name="TestExample1"> <classes> <class name="com.dev2qa.example.testng.parallel.TestNGParallelExample1"/> <class name="com.dev2qa.example.testng.parallel.TestNGParallelExample2"/> </classes> </test> <test name="TestExample2"> <classes> <class name="com.dev2qa.example.testng.parallel.TestNGParallelExample2"/> </classes> </test> </suite>
- Now right click testng1.xml file, click ” Run As —> TestNG Suite ” menu item.
- You can see web browser Chrome, Firefox, IE opened and closed one by one. And you can see below output in Eclipse console, all the test method executor thread id are same.
Example 2 : Execute Test Methods In Parallel ( Multiple Thread ).
- Change the parallel value to “methods”.
- Run testng1.xml again. You can see Chrome and Firefox browser will be opened simultaneous because
testChrome()
andtestFirefox()
are all in classTestNGParallelExample1
. And IE browser will be opened after them, becausetestIE()
is in another classTestNGParallelExample2
. - From the console output, you can also see the thread id to execute above methods are totally different.
Example 3 : Execute Test In Parallel ( Multiple Thread ).
- Change the parallel value to “tests”.
- Run testng1.xml again, you will see all the test methods in TestExample1 will use one thread and methods in TestExample2 will use another thread.
Example 4 : Execute Test Class In Parallel ( Multiple Thread ).
- Change the parallel value to “classes”.
- Run testng1.xml again, you can see below output. You can see in one test, same class’s methods will run in same thread.
Example 5 : Execute Test Instance In Parallel ( Multiple Thread ).
- Change the parallel value to “instances”.
- Run testng1.xml again, you will see below output. Because in one test, each class will be treated as one testng instance, so in TestExample1, testChrome and testFirefox in TestNGParallelExample1 class will use the same thread.
[download id=”2169″]