javaseleniumannotationstestnginvocation

How to set invocation count during execution for the method using TestNG & Java


I am having one test method, which I am manually executing for 200 times.

@Test(priority=2, invocationCount = 200)
public void inviteTalents() throws InterruptedException
{
  logger.log(Status.INFO, "Count " + logins[count]);
}

How can I set the invocation count variable? I have tried something like this, but doesn't work. Any help?

  @BeforeMethod
  public void setUp(Method method, ITestContext context) {

    if(method.getName().equals("test3"))
    {
        ITestNGMethod currentTestNGMethod = null;
        for (ITestNGMethod testNGMethod : context.getAllTestMethods())
        {
          if (testNGMethod.getInstance() == this)
          {
            currentTestNGMethod = testNGMethod;
            break;
          }
        }
        currentTestNGMethod.setInvocationCount(count);  
    }
  }

Solution

  • You can make use an IAnnotationTransformer implementation to get this done.

    Here's a sample that shows how to pass in the method name and the invocation count via JVM arguments and how the annotation transformer implementation changes the invocation count in runtime.

    package com.rationaleemotions.stackoverflow.qn51160440;
    
    import org.testng.ITestResult;
    import org.testng.Reporter;
    import org.testng.annotations.Test;
    
    public class TestClassSample {
      @Test
      public void fooTest() {
        ITestResult r = Reporter.getCurrentTestResult();
        String methodname = r.getMethod().getMethodName();
        System.err.println(
            "Running " + methodname + "() on Thread [" + Thread.currentThread().getId() + "]");
      }
    }
    

    Here's how the annotation transformer looks like

    package com.rationaleemotions.stackoverflow.qn51160440;
    
    import java.lang.reflect.Constructor;
    import java.lang.reflect.Method;
    import org.testng.IAnnotationTransformer;
    import org.testng.annotations.ITestAnnotation;
    
    public class AnnotationTransformerImpl implements IAnnotationTransformer {
    
      @Override
      public void transform(
          ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
        //Pass the value via JVM argument -Dkvp=someMethod=400
        //Here "someMethod" is the name of the method and 400 is the invocation count value
        String kvp = System.getProperty("kvp", "fooTest=200");
        String keyValue[] = kvp.split("=");
        if (keyValue.length != 2) {
          return;
        }
        if (!testMethod.getName().equalsIgnoreCase(keyValue[0])) {
          return;
        }
        annotation.setInvocationCount(Integer.parseInt(keyValue[1]));
        annotation.setThreadPoolSize(25);
      }
    }
    

    Here's how the suite file looks like

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
    <suite name="45160355_Suite" parallel="methods" verbose="2" >
        <listeners>
            <listener
              class-name="com.rationaleemotions.stackoverflow.qn51160440.AnnotationTransformerImpl"/>
        </listeners>
        <test name="45160355_test" verbose="2">
            <classes>
                <class name="com.rationaleemotions.stackoverflow.qn51160440.TestClassSample"/>
            </classes>
        </test>
    </suite>