How to run same testng class from testNG suite

Skip to first unread message

MCD dotCom

Jul 28, 2022, 6:31:20 PM7/28/22
to testng-users

I like to call the same testNG class multiple times from testNG suite. When I do that, it does not run the same testNG class second time. 


<!DOCTYPE suite SYSTEM "">
<suite name="Test-Suite">
    <test name="test">
            <class name="tests.TC001_Test1" /> <!-- runs -->
            <class name="tests.TC001_Test2" />  <!-- runs -->
            <class name="tests.TC001_Test1" /> <!-- skips --- this test does not run. it gets skipped because it is the same testNG class as the first one -->


How can I run the same testNG class multiple times from testNG suite?

⇜Krishnan Mahadevan⇝

Jul 30, 2022, 6:36:04 AM7/30/22
to testng-users
Here's one approach.

First define an annotation that looks like below

import static java.lang.annotation.ElementType.METHOD; import static java.lang.annotation.ElementType.TYPE; import java.lang.annotation.Retention; import java.lang.annotation.Target; @Retention(java.lang.annotation.RetentionPolicy.RUNTIME) @Target({METHOD, TYPE}) public @interface Repeat { int times() default 1; }

Now use the annotation in your test class either at the class level or at the method level as below.

import org.testng.ITestResult; import org.testng.Reporter; import org.testng.annotations.Test; @Repeat(times = 10) public class SampleTestClass { @Test public void testMethod1() { System.err.println("running testMethod1 for iteration # " + invocationCount()); } @Test public void testMethod2() { System.err.println("running testMethod2 for iteration # " + invocationCount()); } private static int invocationCount() { ITestResult itr = Reporter.getCurrentTestResult(); return itr.getMethod().getCurrentInvocationCount(); } }

In order for your tests to be repeatable, now define an annotation transformer that looks like below

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import org.testng.IAnnotationTransformer;
import org.testng.annotations.ITestAnnotation;

public class RepeatableTest implements IAnnotationTransformer {

  public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor,
      Method testMethod) {
    if (testClass != null) {
      Repeat repeat = (Repeat) testClass.getAnnotation(Repeat.class);
      if (repeat == null) {
        //If annotation was not found at class level then return.
    if (testMethod == null) {
    Repeat repeat = testMethod.getAnnotation(Repeat.class);
    if (repeat == null) {
      //If we didnt find the annotation at the method level then search for the annotation
      // at the class level to which the method belongs to
      repeat = testMethod.getDeclaringClass().getAnnotation(Repeat.class);
      if (repeat == null) {

Now finally define your test suite that looks like below.

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM ""> <suite name="my_suite" verbose="2"> <listeners> <listener class-name="com.rationaleemotions.repeat.RepeatableTest"/> </listeners> <test name="my_test" verbose="2"> <classes> <class name="com.rationaleemotions.repeat.SampleTestClass"/> </classes> </test> </suite>

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @
My Technical Scribblings @

You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To view this discussion on the web visit
Reply all
Reply to author
0 new messages