HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / TestNG / TestNG parallel execution of tests, classes and suites

TestNG parallel execution of tests, classes and suites

TestNG parallel execution of tests, classes and suites with examples. Learn how to run testng tests and suites in parallel or single test in multiple threads.

Parallelism or multi-threading in software terms is defined as the ability of the software, operating system, or program to execute multiple parts or sub-components of another program simultaneously. 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.

Table Of Contents

1. Advantages of parallel rests execution
2. Run parallel testcases
3. Run test classes in parallel
4. Run tests suite in parallel
5. Configure a testcase to run in multiple threads

1. Advantages of parallel rests execution

Parallelism or multi-threaded execution can provide a lot of advantages to the users. The following are two:

  1. Reduces execution time – As tests are executed in parallel, multiple tests get executed simultaneously, hence reducing the overall time taken to execute the tests.
  2. Allows multi-threaded tests – Using this feature, we can write tests to verify certain multi-threaded code in the applications.

Parallel test execution is vastly used by the QA industry for functional automation testing. This feature helps QA to configure their tests to be executed easily in multiple browsers or operating systems simultaneously.

There are different ways in which parallelism feature can be configured in TestNG.

2. Run parallel testng testcases

TestNG provides multiple ways to execute the tests in a multi-threaded condition, one of them is executing each test method in a single thread. This mode reduces the execution time significantly because more tests are executed in parallel, hence reducing the total execution time.

Java program for parallel test execution in testng. This is an example of how to run methods in parallel using testng xml from single class file.

package com.howtodoinjava.parallelism;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class ParallelMethodTest 
{
	@BeforeMethod
	public void beforeMethod() {
		long id = Thread.currentThread().getId();
		System.out.println("Before test-method. Thread id is: " + id);
	}

	@Test
	public void testMethodsOne() {
		long id = Thread.currentThread().getId();
		System.out.println("Simple test-method One. Thread id is: " + id);
	}

	@Test
	public void testMethodsTwo() {
		long id = Thread.currentThread().getId();
		System.out.println("Simple test-method Two. Thread id is: " + id);
	}

	@AfterMethod
	public void afterMethod() {
		long id = Thread.currentThread().getId();
		System.out.println("After test-method. Thread id is: " + id);
	}
}

The preceding test class contains two test methods, which prints a message onto the console when executed. The ID of the thread on which the current method is being executed is evaluated using the Thread.currentThread.getId() code.

It also contains the before and after methods, which also prints the thread ID of the current thread onto the console when executed.

Create a new file named methods-test-testng.xml under the project and write below code.

<suite name="Test-method Suite" parallel="methods" thread-count="2" >
  <test name="Test-method test" group-by-instances="true">
    <classes>
      <class name="com.howtodoinjava.parallelism.ParallelMethodTest" />
    </classes>
  </test>
</suite>

Select this file in Eclipse and run it as a TestNG suite. You will see the following test result in the Console window:

Before test-method. Thread id is: 10
Before test-method. Thread id is: 9
Simple test-method Two. Thread id is: 10
Simple test-method One. Thread id is: 9
After test-method. Thread id is: 10
After test-method. Thread id is: 9
Note that the Id value shown in the previous screenshot may not be the same in your console output. The Id value is assigned at runtime by the Java virtual machine (JVM) during execution.

The previous test result clearly shows that each test method and its respective before and after method is executed in a different thread. This is identified by the ID of the thread that is printed on the console.

2. Run testng test classes in parallel

In this example, we will learn about executing testng test classes in parallel; each test class that is part of the test execution will be executed in its own thread.

public class ParallelClassesTestOne 
{
	@BeforeClass
	public void beforeClass() {
		long id = Thread.currentThread().getId();
		System.out.println("Before test-class. Thread id is: " + id);
	}

	@Test
	public void testMethodOne() {
		long id = Thread.currentThread().getId();
		System.out.println("Sample test-method One. Thread id is: " + id);
	}

	@Test
	public void testMethodTwo() {
		long id = Thread.currentThread().getId();
		System.out.println("Sample test-method Two. Thread id is: " + id);
	}

	@AfterClass
	public void afterClass() {
		long id = Thread.currentThread().getId();
		System.out.println("After test-class. Thread id is: " + id);
	}
}
public class ParallelClassesTestTwo 
{
	@BeforeClass
	public void beforeClass() {
		long id = Thread.currentThread().getId();
		System.out.println("Before test-class. Thread id is: " + id);
	}

	@Test
	public void testMethodOne() {
		long id = Thread.currentThread().getId();
		System.out.println("Sample test-method One. Thread id is: " + id);
	}

	@Test
	public void testMethodTwo() {
		long id = Thread.currentThread().getId();
		System.out.println("Sample test-method Two. Thread id is: " + id);
	}

	@AfterClass
	public void afterClass() {
		long id = Thread.currentThread().getId();
		System.out.println("After test-class. Thread id is: " + id);
	}
}

Create a new file named classes-test-testng.xml under the project and write below code.

<suite name="Test-class Suite" parallel="classes" thread-count="2" >
  <test name="Test-class test" >
    <classes>
      <class name="com.howtodoinjava.parallelism.ParallelClassesTestOne" />
      <class name="com.howtodoinjava.parallelism.ParallelClassesTestTwo" />
    </classes>
  </test>
</suite>

Select this file in Eclipse and run it as a TestNG suite. You will see the following test result in the Console window:

Before test-class. Thread id is: 10
Before test-class. Thread id is: 9
Sample test-method One. Thread id is: 9
Sample test-method One. Thread id is: 10
Sample test-method Two. Thread id is: 10
After test-class. Thread id is: 10
Sample test-method Two. Thread id is: 9
After test-class. Thread id is: 9

The previous test result clearly shows that each test class and its respective beforeClass and afterClass methods are executed in a different thread. This is identified by the id of the thread that is printed on the console.

4. TestNG run parallel suites

Let’s learn about executing each test inside a suite in parallel, that is, each test that is part of the test suite execution will be executed in its own separate respective thread.

package com.howtodoinjava.parallelism;

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class ParallelSuiteTest 
{
	String testName = "";

	@BeforeTest
	@Parameters({ "test-name" })
	public void beforeTest(String testName) {
		this.testName = testName;
		long id = Thread.currentThread().getId();
		System.out.println("Before test " + testName + ". Thread id is: " + id);
	}

	@BeforeClass
	public void beforeClass() {
		long id = Thread.currentThread().getId();
		System.out.println("Before test-class " + testName + ". Thread id is: "
				+ id);
	}

	@Test
	public void testMethodOne() {
		long id = Thread.currentThread().getId();
		System.out.println("Sample test-method " + testName
				+ ". Thread id is: " + id);
	}

	@AfterClass
	public void afterClass() {
		long id = Thread.currentThread().getId();
		System.out.println("After test-method  " + testName
				+ ". Thread id is: " + id);
	}

	@AfterTest
	public void afterTest() {
		long id = Thread.currentThread().getId();
		System.out.println("After test  " + testName + ". Thread id is: " + id);
	}
}

Create a new file named suite-test-testng.xml under the project and write below code.

<suite name="Test-class Suite" parallel="tests" thread-count="2">
    <test name="Test-class test 1">
        <parameter name="test-name" value="test-method One" />
        <classes>
            <class name="com.howtodoinjava.parallelism.ParallelSuiteTest" />
        </classes>
    </test>
    <test name="Test-class test 2">
        <parameter name="test-name" value="test-method One" />
        <classes>
            <class name="com.howtodoinjava.parallelism.ParallelSuiteTest" />
        </classes>
    </test>
</suite>

Select this file in Eclipse and run it as a TestNG suite. You will see the following test result in the Console window:

Before test Test One. Thread id is: 9
Before test Test Two. Thread id is: 10
Before test-class Test One. Thread id is: 9
Before test-class Test Two. Thread id is: 10
Sample test-method Test One. Thread id is: 9
Sample test-method Test Two. Thread id is: 10
After test-method  Test Two. Thread id is: 10
After test-method  Test One. Thread id is: 9
After test  Test One. Thread id is: 9
After test  Test Two. Thread id is: 10

The previous test result clearly shows that each test in a suite is executed in its respective thread. This is identified by the ID of the thread that is printed on the console.

5. Configure testng test to run in multiple threads

Earlier we discussed how to run classes, methods, and tests in parallel or in multi-threaded mode. TestNG also provides the flexibility to configure a test method to be run in a multi-threaded environment. This is achieved by configuring it while using the @Test annotation on a method.

public class IndependentTest 
{
	@Test(threadPoolSize = 3, invocationCount = 6, timeOut = 1000)
	public void testMethod() 
	{
		Long id = Thread.currentThread().getId();
		System.out.println("Test method executing on thread with id: " + id);
	}
}

The method is configured to run in multi-threaded mode by using the threadPoolSize attribute along with the Test annotation. The value of the threadPoolSize is set to 3; this configures the test method to be run in three different threads.

The other two attributes, invocationCount and timeOut, configures the test to be invoked a multiple number of times and fail if the execution takes more time.

Create a new file named independent-test-testng.xml under the project and write below code.

<suite name="Independent test Suite" >
  <test name="Independent test">
    <classes>
     <class name="com.howtodoinjava.parallelism.IndependentTest" />
    </classes>
  </test>
</suite>

Select this file in Eclipse and run it as a TestNG suite. You will see the following test result in the Console window:

Test method executing on thread with id: 11
Test method executing on thread with id: 10
Test method executing on thread with id: 9
Test method executing on thread with id: 11
Test method executing on thread with id: 11
Test method executing on thread with id: 10

Here, test method is executed multiple times based on the invocationCount attribute value. Each execution is done in a separate thread that is clearly visible from the test report output. This feature is useful when you want to run only a fixed number of test methods in multi-threaded mode and not the whole test suite.

Happy Learning !!

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. harsha

    October 15, 2019

    How to Run suites in Parallel.

  2. Pedro Garcia

    May 22, 2019

    Hello, is it possible to have several classes with multiple testNG and create a main class that runs all the classes we want? And the results are in a single .html file?
    I created a class with several tests in a total of 31 but separated by @test, but if I want to run only specific tests I will have to search and comment on the ones I do not want to run, is there any way to do it in a simpler way? (I managed to do this for a .txt file where I created a main code, and in that code I call the classes name and I run the ones I want, but I do not know how to do this with testNG).

  3. shans

    March 9, 2019

    hey !
    I’m new to selenium. I’m automating a website, which has 6 products. i wrote the selenium code for each of the product in the testNG class. I’m maintaing a testng.xml , i want to run all my 6 java testng classes sequentially using testng.xml, but is not happening. If i run the testng.xml using classes then only my first class gets executed and the rest gives the error as “Illegal state execpetion”. Can anyone please help me.

    • Samreen Chughtai

      March 14, 2019

      Hi Shans,
      I can help you if you describe what you are trying to do in your class. Better show the code. Probably you need to set your browser properties before every testcase.
      Post the code snippet and I can help you.

  4. Neha Jain

    July 25, 2018

    I was trying “Configuring a test method to run in multiple threads”.

    My tests still run in sequence instead of parallel. I tried all the approaches, and everything is working except the last one.

    I have to write a performance test suite to check the load an API can take.

    • Samreen

      March 14, 2019

      Hi Neha, Hope you have tried Postman for testing API performance. It is a better approach than testing using selenium.

  5. Mohan Kumar

    January 17, 2018

    Hi All,

    I have two testng xml files(testng1.xml and testng2.xml) and I need to launch one Chrome browser per xml file in parallel(means like 1 chrome driver for 1 xml suite). How to achieve this..?

  6. Vikram VI

    July 28, 2017

    Hi,

    Thanks for explaining in simple language.

    Can you please clarify wrt “Running tests inside a suite in parallel”

    – I want to do some action which is common to all the tests but only once, how to achieve it ?
    – This action is common to all the tests so I don’t want to run in parallel but only once for all the tests / classes.

    Regards,
    Vikram

    • Samreen Chughtai

      March 14, 2019

      Use beforeClass annotation

  7. Eswar

    February 8, 2017

    hi Lokesh,
    Can i use this parallel testing to test api using rest assured by using testng

  8. Uma

    September 30, 2016

    Great blog! Informative. Thank you

  9. Gopika

    August 30, 2016

    Hii
    I hav 10 testcases I hav to run 6 testcases parallel and 4 testcases sequential how can I do tat help me…..

  10. Himansu

    July 26, 2016

    Hi Lokesh,
    We can also change the above example to executed test both parallel and sequentially on the same execution flow.

  11. Shru

    July 6, 2016

    Hi Lokesh,

    Firstly, this is a very informative post! Thank you. I had a query, I want to run the test suites in parallel. Is there a way to do that? Please let me now if you have any suggestions to do so.

    Thanks,
    Shru

  12. Srinivas

    March 30, 2016

    Hi,

    We have configured for parallel suite from TestNG. The problem is that is opens two browsers but execution happens only in one browser. Can someone help me with this ?

    • Lokesh Gupta

      March 30, 2016

      A similar problem is discussed here.

    • Govardhan

      April 28, 2016

      Check in your class that whether you are creating driver instance in both BeforeClass & BeforeMethod. If so, it opens 2 browsers but execution happen in the browser that opened in BeforeMethod. If driver instance is created in single method, please share your class and .xml files for better understanding of your issue.

  13. Bala

    February 11, 2016

    Hi Lokesh ,

    i am facing issue on running parallel= “tests”

    Issue Facing: I have created all test cases in one class files at @Test each, Now i want to run the class file in different browser in parallel

    XML file:

    But when i executed this xml file only test cases running in FF browser , whereas chrome browser launched and home url loads and Testcases not executed it strucks there itself.

    Can you please help me to get out of this issue. Thanks in advance.

    • Lokesh Gupta

      February 11, 2016

      Please post sourcecode inside [xml] … [/xml] tags.

      • Bala

        February 11, 2016

        Hi Lokesh,
        Can you please provide your mail id , i can’t able to send XML code here

        • Lokesh Gupta

          February 11, 2016

          howtodoinjava@gmail.com

          • Bala

            February 11, 2016

            Lokesh i mailed you can you please look into it.

    • Aman

      March 12, 2020

      Hi Bala ,

      Have you got any solution for this as I am also having same issue ?
      please let me know solution.

  14. Joe

    October 2, 2015

    I am using a similar set up, but my @BeforeTest method seems to start two tests on the same thread, any thoughts why?

    
      @BeforeTest(alwaysRun = true)
        @Parameters({ "selenium.OS", "selenium.browser","selenium.testClassNameHere" })
        public void beforeTest(String OS, String browser, String testClassNameHere) {
            Out.trace("Starting beforeTest for "+testClassNameHere+" on thread:  " + Thread.currentThread().hashCode());
            //create driver here
            Out.trace("beforeTest has finished");
        }
    
    
    However, here is the log, showing that two of the test classes started on the same thread, which causes all sorts of headaches with listeners and reporting.
    
       [testng] TRACE: Starting beforeTest for AcceptanceTests.ErrorReportingTests on thread:  777376239
       [testng] TRACE: Starting beforeTest for AcceptanceTests.PopupTests on thread:  777376239
       [testng] TRACE: Starting beforeTest for AcceptanceTests.LoginPageTests on thread:  1235740568
       [testng] TRACE: Finished Making driver, Timeout set to 0 seconds.
       [testng] TRACE: Finished Making driver, Timeout set to 0 seconds.
       [testng] TRACE: beforeTest has finished
       [testng] TRACE: beforeTest has finished
    
    

    Here is the xml file showing three test classes…

    <?xml version="1.0"?>
    <suite name="Debugging QA Tests" parallel="tests" thread-count="10">
    <test name="Popup Tests" preserve-order="true" >
            <parameter name="selenium.OS" value="localhost" />
            <parameter name="selenium.browser" value="chrome" />
            <parameter name="selenium.testClassNameHere" value="AcceptanceTests.PopupTests" />
            <classes>
                <class name="qaautomation.AcceptanceTests.PopupTests" />
            </classes>
        </test>
      <test name="Login Page Tests" preserve-order="true">
            <parameter name="selenium.OS" value="localhost" />
            <parameter name="selenium.browser" value="chrome" />
            <parameter name="selenium.testClassNameHere" value="AcceptanceTests.LoginPageTests" />
            <classes>
                <class name="qaautomation.AcceptanceTests.LoginPageTests" />
            </classes>
        </test>
        <test name="Error Reporting Tests" preserve-order="true">
            <parameter name="selenium.OS" value="localhost" />
            <parameter name="selenium.browser" value="chrome" />
           <parameter name="selenium.testClassNameHere" value="AcceptanceTests.ErrorReportingTests" />
             <classes>
                <class name="qaautomation.AcceptanceTests.ErrorReportingTests" />
            </classes>
        </test>
    </suite> 
    
    
    • Lokesh Gupta

      October 4, 2015

      Hi Joe, Yes TestNG first tries to find a idle thread and only tries to create new if found none idle.

      • Joe

        October 5, 2015

        I thought the whole idea of parallel=”tests” was that each class would start on a separate thread? Is there no way to force TestNG to start each test class in a separate thread? The problem is that I using this to run selenium grid, and if the test classes are not each on a separate thread, the driver can get shared between classes, and this seems to cause all sorts of problems.

  15. davyjones2010

    January 3, 2015

    Hi Lokesh, this is really an informative post. But I realized that maybe you’ve wrongly post the content of “suite-test-testng.xml” in “Running tests inside a suite in parallel” section. It is the same with the “classes-test-testng.xml” in “Running test classes in parallel” section, and thus, it will not generate the desired result.

    • davyjones2010

      January 3, 2015

      And I suspect the correct suite-test-testng.xml should be as following:

      <?xml version="1.0" encoding="UTF-8"?>
      <suite name="Test-class Suite" parallel="tests" thread-count="2">
          <test name="Test-class test 1">
              <parameter name="test-name" value="test-method One" />
              <classes>
                  <class name="com.howtodoinjava.parallelism.ParallelSuiteTest" />
              </classes>
          </test>
          <test name="Test-class test 2">
              <parameter name="test-name" value="test-method One" />
              <classes>
                  <class name="com.howtodoinjava.parallelism.ParallelSuiteTest" />
              </classes>
          </test>
      </suite>
      
      • Alfredo

        February 9, 2015

        I agree

        • Lokesh Gupta

          February 10, 2015

          Looks like while drafting these examples in form of tutorial, I messed up. Thanks for the patience.

Comments are closed on this article!

Search Tutorials

TestNG Tutorial

  • TestNG – Introduction
  • TestNG – Hello World
  • TestNG – Maven
  • TestNG – Annotations
  • TestNG – Expected Exception
  • TestNG – Disable/Ignore Tests
  • TestNG – Parallel Tests
  • TestNG – Dependent Tests
  • TestNG – Timeout Tests
  • TestNG – @Parameters
  • TestNG – @DataProvider
  • TestNG – @Factory
  • TestNG – @DataProvider
  • TestNG – Before and After
  • TestNG – Test Groups

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces