Run TestNG test sequentially with invocationCount

359 Views Asked by At

I have a long-running @test annotated method that I need to run repeatedly and I achieve this with invocationCount.

When I hard code the value e.g. invocationCount=10 then it runs succesfully, running tests 1 - 10 sequentially, i.e. it waits for the method to complete and then starts the next invocation.

However, I need to be able to alter the invocationCount value at runtime so have implemented the IAnnotationTransformer interface to achieve this, passing in the key, value pair at runtime. However, this has the effect of running the tests in parallel. I've provided a simplified version here:

TestNG.xml:

<suite name="Run tests sequentially" verbose="1" thread-count="1" configfailurepolicy="continue">

    <listeners>
        <listener
                class-name="com.tests.utils.AnnotationTransformerImpl"/>
    </listeners>

    <test name="invocation-test-synchronised" parallel="false">

        <classes>
            <class name="com.tests.journeys.InvocationTest"/>
        </classes>
    </test>
</suite>

AnnotationTransformer: public class AnnotationTransformerImpl implements IAnnotationTransformer {

    private String invocation_key = System.getProperty("invKey");
    private String invocation_count = System.getProperty("invCount");
    //value passed from JVM is -Dkvp=invocationTest=5

    @Override
    public void transform(
         ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
        String kvp = System.getProperty(invocation_key,invocation_count);
        String keyValue[] = kvp.split("=");
        if (keyValue.length != 2) {
            return;
        }
        if (!testMethod.getName().equalsIgnoreCase(keyValue[0])) {
            return;
        }
        annotation.setInvocationCount(Integer.parseInt(keyValue[1]));
        annotation.setThreadPoolSize(5);
    }
}

Test class: public class InvocationTest {

    @Test(invocationCount = 5)
    public void invocationTest() {

        ITestResult r = Reporter.getCurrentTestResult();
        String methodname = r.getMethod().getMethodName();
        System.err.println(
                "Running " + methodname + "() on Thread [" + Thread.currentThread().getId() + "]");

        Thread.sleep(5000);
    }

}

The resulting test output runs the test method in parallel, on separate threads:

IntelliJ_run

How can I achieve running a method to completion before starting the next invocation? I know it will be in the documentation somewhere, just struggling.

1

There are 1 best solutions below

0
Villa_7 On

Try to google: Java multithreading. It will give you a solution. Probably with using Thread.join(); and other stuff.