Parallel Test Execution Using TestNG

Sometimes we need to execute the Test Cases in parallel to same the Execution time. We can do this in TestNG with the help of small amendment in “testng.xml” file. TestNG provides inherent support to run the tests in parallel.

 

TestNG allows the tests to run in parallel or multi-threaded mode. This means that based on the Test suite configuration, different threads are started simultaneously and the test methods are executed in them. This gives a user a lot of advantages over normal execution, mainly reduction in execution time and ability to verify a multi-threaded code.

 

Video Tutorial:

If you liked this video, then please subscribe to our YouTube Channel for more video tutorials.

 

Running Test Methods in Parallel

TestNG provides multiple ways to execute the test cases in parallel, We can execute each test case in a single thread. This mode reduces the execution time significantly because more tests are executed in parallel, hence reducing the total execution time.

 

We can achieve this using “parallel” and “thread-count” keywords in Suite tag inside the “testng.xml” file.

 

Lets modify our testng.xml file. We add parallel and thread-count keyword in our Suite tag.

<!DOCTYPE suite SYSTEM “http://testng.org/testng-1.0.dtd” >
  
<suite name=”TestGroupSuite” parallel=”methods” thread-count=”2″>

  <listeners>
      <listener class-name=”org.uncommons.reportng.HTMLReporter”/>
      <listener class-name=”org.uncommons.reportng.JUnitXMLReporter”/>
  </listeners>

  <test name=”Test With Priority” >
    
    <classes>
       <class name=”com.Seleniumbix.TestNG.SampleParallelMethods” />
    </classes>
  </test>

</suite>

Parallel Execution in TestNG

You can notice the parallel execution in above output. TestNG invoked two threads and both executed concurrently.

 

Running Test Classes in Parallel

We can also execute the different Test classes parallel as well. We need to modify our testng.xml file. Let’s understand this with simple example –

First Test Class-

 

Second Test class —

 

Let’s have a look at modified testng.xml file to run the both classes parallel.

<!DOCTYPE suite SYSTEM “http://testng.org/testng-1.0.dtd” >
  
<suite name=”TestGroupSuite” parallel=”classes” thread-count=”2″>
  <listeners>
      <listener class-name=”org.uncommons.reportng.HTMLReporter”/>
      <listener class-name=”org.uncommons.reportng.JUnitXMLReporter”/>
  </listeners>
  <test name=”Test With Priority” >
    
    <classes>
       <class name=”com.Seleniumbix.TestNG.SampleClassFirst” />
       <class name=”com.Seleniumbix.TestNG.SampleClassSecond” />
    </classes>
  </test>
</suite>

Parallel Execution in TestNG

So this is the way, How we can execute the Test Case or Test Classes in parallel to each other. You may declare any number in Thread-count. TestNG invokes the threads as per the given number in testng.xml file.

0 Comment

Leave a Reply