Parallel testing code samples (Java SDK)

Note: UFT Developer supports parallel testing with TestNG and NUnit 3. You can also use your own custom framework if it can launch tests in parallel.

Run multiple invocations of the same test method in parallel

This example demonstrates multiple invocations of the test method in parallel threads.

/**
* The test method that will be invoked multiple times.
*  @invocationCount The number of times this method should be invoked.
*  @threadPoolSize The size of the thread pool for this method.
*   The method will be invoked from multiple threads as specified by invocationCount.
*   This attribute is ignored if invocationCount is not specified.
*/
@Test(invocationCount = 10, threadPoolSize = 3)
public void testMultiInvocation() throws ReportException {
	String msg = "MultiInvocation: Thread id = " + Thread.currentThread().getId();
	Reporter.reportEvent(msg, msg);
}

Back to top

Run multiple classes or methods in parallel

This example demonstrates running multiple classes or methods in parallel. It should be used together with the XML suite file that defines the parallel execution mode (methods / tests / classes / instances).

The parallel run project should contain several test classes (for example, Parallel1, Parallel2, etc.) each containing several test methods to be run in parallel.

  • Use the testng-parallel-methods.xml suite file to run all test methods in separate threads (up to a defined thread count).
  • Use the testng-parallel-classes.xml suite file to run all the methods in the same class in the same thread, but each class in a separate thread.
/**
* The test method uses the Chrome browser as the AUT.
*/
@Test
public void testChrome_1() throws ReportException, GeneralLeanFtException { testBrowser(BrowserType.CHROME, "CHROME_1"); } /** * The test method uses the Internet Explorer browser as the AUT. */ @Test
public void testIE_1() throws ReportException, GeneralLeanFtException { testBrowser(BrowserType.INTERNET_EXPLORER, "IE_1"); } private void testBrowser(BrowserType browserType, String text) throws ReportException, GeneralLeanFtException { String url = "https://www.advantageonlineshopping.com/#/"; Browser browser = BrowserFactory.launch(browserType); browser.navigate(url);        browser.describe(Link.class, new XPathDescription("//A[@id=\"menuUserLink\"]")).click(); EditField userNameField = browser.describe(EditField.class, new EditFieldDescription.Builder().type("text").name("userName").build()); userNameField.setValue(text); Verify.areEqual(userNameField.getValue(), text); }

In testng-parallel-methods.xml, run all test methods in separate threads (up to a defined thread count):

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

<suite name="Parallel methods suite" parallel="methods" thread-count="6" >
	<test name="Parallel methods test" >
	  <classes>
		<class name="parallel.Parallel1" /> 
		<class name="parallel.Parallel2" />
		<class name="parallel.Parallel3" />
	  </classes>
	</test>
</suite>

In testng-parallel-classes.xml, run all the methods in the same class in the same thread, but each class in a separate thread.

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

<suite name="Parallel classes suite" parallel="classes" thread-count="3" >
	<test name="Parallel classes test" >
	  <classes>
		<class name="parallel.Parallel1" />
		<class name="parallel.Parallel2" />
		<class name="parallel.Parallel3" />
	  </classes>
	</test>
</suite>

Back to top

Run data provider tests in parallel

The following is an example of a data provider test running in parallel on multiple browsers.

/**
* The test method. 
* @Test Marks the method as part of the test.
* @dataProvider The name of the data provider for this test method.
*/
@Test(dataProvider="BrowserTypes")
public void parallelDataProviderTest(BrowserType browserTypeToLaunch) throws Exception {
	Browser browser = BrowserFactory.launch(browserTypeToLaunch);
	browser.navigate("www.hpe.com");
	browser.sync();
	Reporter.reportEvent("Info", "Browser launched: " + browserTypeToLaunch + ", Thread id = " + Thread.currentThread().getId());
	browser.close();
} 

/**
* The method that provides data for the test method. 
* @return An Object[][] where each Object[] can be assigned the parameter list of the test method.
* @DataProvider Marks a method as providing data for a test method. The @Test method that wants to receive
* data from this DataProvider needs to use a dataProvider name equivalent to the name of this annotation.
*  @name The name of this data provider. If not supplied, the name of this data provider will automatically be set to the name of the method.
*  @parallel If set to true, tests generated using this data provider are run in parallel. Default value is false.
*/
@DataProvider(name="BrowserTypes", parallel=true)
public Object[][] dp2() {
	return new Object[][]{
		{BrowserType.CHROME},
		{BrowserType.INTERNET_EXPLORER},
		{BrowserType.FIREFOX}
	};
}

Back to top

See also: