anupam kumar
anupam kumar

Reputation: 11

Set (parallel = false/true) from command line for DataProvider in scenarios method AbstractTestNGCucumberTests

I have Spring boot, Cucumber-TestNG setup have overridden the AbstractTestNGCucumberTests class scenarios method

public abstract class AbstractCucumberParallelTests extends AbstractTestNGCucumberTests {

  @Override
  @DataProvider(parallel = false)
  public Object[][] scenarios() {
    return super.scenarios();
  }
}

I want to set the boolean value as true or false from the command line in the data provider.

Is there any way to parameterize the Data Provider annotation parameters?

Something like this:

@Override
  @DataProvider(parallel = $spring.parallel)
  public Object[][] scenarios() {
    return super.scenarios();
  }

Upvotes: 1

Views: 720

Answers (1)

Krishnan Mahadevan
Krishnan Mahadevan

Reputation: 14746

Here's how you go about doing it.

  1. Create an implementation of org.testng.IAnnotationTransformer that kind of looks like below:
import java.lang.reflect.Method;
import org.testng.IAnnotationTransformer;
import org.testng.annotations.IDataProviderAnnotation;

public class SimpleTransformer implements IAnnotationTransformer {

  @Override
  public void transform(IDataProviderAnnotation annotation, Method method) {
    boolean runInParallel = Boolean.getBoolean("parallel.run");
    if (runInParallel) {
      annotation.setParallel(true);
    }
  }
}

Now you can refer to this TestNG listener in your suite file as 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.qn68650160.SimpleTransformer"/>
  </listeners>
  <test name="my_test" verbose="2">
    <classes>
      <class name="com.rationaleemotions.qn68650160.SampleTestCase"/>
    </classes>
  </test>
</suite>

and then you can run your tests by passing the JVM argument -Dparallel.run=true

Alternatively if you need this listener to be executed for all of your tests all the time irrespective of whether its referred to in the suite file or not, you can wire it as a service loader loaded listener as documented in the official documentation in the TestNG site.

Note: I am using TestNG 7.6.1 (latest released version as of today and which needs JDK11)

Upvotes: 1

Related Questions