top of page

Parallel Execution In TestsNG

What Is Parallel Execution In TestNG ?

TestNG allows to run the multiple tests simultaniously rather than one after another.Not only tests, even methods,classes and instances can be run in parallel.

In .xml an attribute parallel is used to run the tests in parallel.

The value of this attribute have to specified as per requirement.

Values taken by attribute parallel :

  • methods - It will run all test methos of classes in parallel.

  • classes - It will run all the classes present inside <classes> tag in parallel.

  • tests - It will run all the test cases present within <test> tag in parallel

  • instances - It will run all the test cases of that instance in parallel.

Threads in testNG :

Threads implies the division of test execution in modules to run in parallel.

In testNG an attribute thread-count  that accepts an interger type value, is used in .xml to achieve multithreading.

The number of threads depends on the number test cases.

Syntax :

<test name="Paralel-Tests" parallel="classes" thread-count="2">

* The deafault value of thread-count is '5' ,hence there's no need to specify thread-count, if the number of test casses is below 5.

Example of method execution in parallel :

 <test name="Paralel-Tests" parallel="methods">

Write test methods to run simultaniously :

package testNGexamples;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class ParallelMethods {
   WebDriver driver;

    @Test
   public void chromeTest() {
       System.setProperty("webdriver.chrome.driver", "..\\TestNGSelenium\\drivers\\chromedriver99.exe");
       driver = new ChromeDriver();
       driver.close();
   }

    @Test
   public void firefoxTest() {
       System.setProperty("webdriver.gecko.driver", "..\\TestNGSelenium\\drivers\\geckodriver.exe");
       driver = new FirefoxDriver();
       driver.close();
   }
}

 

Create .xml for parallel execution :

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parallel Test-Suite">
   <test name="Paralel-Tests" parallel="methods">
       <classes>
           <class name="testNGexamples.ParallelMethods"></class>
       </classes>
   </test>
</suite>

Example of method execution in parallel :

<test name="Paralel-Tests" parallel="classes" thread-count="2">

Create two classes to run parallely :

Class1 :

package testNGexamples;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

public class Class1 {
   WebDriver driver;

    @Test
   public void chromeTest() {
       System.setProperty("webdriver.chrome.driver", "..\\TestNGSelenium\\drivers\\chromedriver99.exe");
       driver = new ChromeDriver();
       driver.quit();
   }
}

 

Class2 :

package testNGexamples;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class Class2 {
   WebDriver driver;

    @Test
   public void firefoxTest() {
       System.setProperty("webdriver.gecko.driver", "..\\TestNGSelenium\\drivers\\geckodriver.exe");
       driver = new FirefoxDriver();
       driver.quit();
   }
}

 

.xml :

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parallel Test-Suite">
   <test name="Paralel-Tests" parallel="classes" thread-count="2">
       <classes>
           <class name="testNGexamples.Class1"></class>
           <class name="testNGexamples.Class2"></class>
       </classes>
   </test>
</suite>

 

Parallel testing at suite level :

We can run all the tests inside the suite at the same time with the value of  parallel attribute changed to tests.

 

Example :

<suite name="Parallel Test-Suite" parallel="tests" thread-count="3">

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parallel Test-Suite" parallel="tests" thread-count="3">
   <test name="Paralel-Tests-1">
       <classes>
           <class name="testNGexamples.Class1"></class>
       </classes>
   </test>
   <test name="Paralel-Tests-2">
       <classes>
           <class name="testNGexamples.Class3"></class>
           <class name="testNGexamples.Class4"></class>
       </classes>
   </test>
</suite>

Refer next page Invocation Count In TestNG

bottom of page