How to run same testng class from testNG suite

43 views
Skip to first unread message

MCD dotCom

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

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. 

testNG.xml:

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Test-Suite">
    <test name="test">
        <classes>
            <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 -->
        </classes>
    </test>


</suite>    

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

⇜Krishnan Mahadevan⇝

unread,
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

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

public class RepeatableTest implements IAnnotationTransformer {

  @Override
  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.
        return;
      }
      annotation.setInvocationCount(repeat.times());
      return;
    }
    if (testMethod == null) {
      return;
    }
    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) {
        return;
      }
    }
    annotation.setInvocationCount(repeat.times());
  }
}

Now finally define your test suite that looks like below.

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd"> <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 @ http://wakened-cognition.blogspot.com/
My Technical Scribblings @ https://rationaleemotions.com/


--
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 testng-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/testng-users/58c9330b-bbf0-4760-b0e0-2e70ca45ee3an%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages