</>

Technology

TestNG

Difficulty

Intermediate

Interview Question

How can we run test cases in parallel using TestNG?

Add parallel and thread-count attributes to the suite tag in testng.xml to run tests simultaneously across threads.

Answer

Running Test Cases in Parallel Using TestNG

TestNG supports parallel execution at method, test, class, or suite level. Add two attributes to the <suite> tag in testng.xml:

XML
<suite name="MySuite" parallel="{methods|tests|classes}" thread-count="{N}">

Parallel modes:

  • methods – Each test method runs in its own thread
  • tests – Each <test> tag in xml runs in its own thread
  • classes – Each test class runs in its own thread
  • instances – Each instance of a class runs in its own thread

Cross-browser parallel example:

Java test class:

Java
import org.testng.Assert;
import org.testng.annotations.*;

public class CrossBrowserTest {

    WebDriver driver = null;

    @Parameters("browser")
    @Test
    public void launchBrowser(String br) {
        if (br.equals("FF")) {
            System.setProperty("webdriver.gecko.driver", "C://Drivers/geckodriver.exe");
            driver = new FirefoxDriver();
        } else if (br.equals("IE")) {
            System.setProperty("webdriver.ie.driver", "C://Drivers/IEDriverServer.exe");
            driver = new InternetExplorerDriver();
        } else if (br.equals("CH")) {
            System.setProperty("webdriver.chrome.driver", "C://Drivers/chromedriver.exe");
            driver = new ChromeDriver();
        }
        driver.get("http://newtours.demoaut.com/");
    }

    @Test(dependsOnMethods = "launchBrowser")
    public void verifyTitle() {
        Assert.assertEquals(driver.getTitle(), "Welcome: Mercury Tours");
    }

    @AfterClass
    public void closeBrowser() {
        driver.quit();
    }
}

testng.xml for parallel cross-browser:

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

<suite name="paralleltesting" parallel="tests" thread-count="5">

  <test name="FirefoxTest">
    <parameter name="browser" value="FF" />
    <classes>
      <class name="parameterization.CrossBrowserTest" />
    </classes>
  </test>

  <test name="ChromeTest">
    <parameter name="browser" value="CH" />
    <classes>
      <class name="parameterization.CrossBrowserTest" />
    </classes>
  </test>

  <test name="IETest">
    <parameter name="browser" value="IE" />
    <classes>
      <class name="parameterization.CrossBrowserTest" />
    </classes>
  </test>

</suite>

Important: Use ThreadLocal<WebDriver> when running in parallel to avoid thread-safety issues:

Java
ThreadLocal<WebDriver> tlDriver = new ThreadLocal<>();

public WebDriver getDriver() {
    return tlDriver.get();
}

Key points:

  • parallel="tests" with thread-count="3" runs 3 <test> blocks simultaneously
  • Combine with Selenium Grid to run on multiple machines
  • Always use ThreadLocal<WebDriver> for thread-safe parallel execution

Follow AutomateQA

Related Topics