Joe Caruso
Joe Caruso

Reputation: 1394

Using groups with TestNG Factory

I have a @Factory method that constructs an array of test class files, and returns them. For this Factory though I want to be able to specify which groups will run, for the entire factory construction.

I wish it was a simple as the following:

CustomObject param = new CustomObject();

@Factory(groups = "a group name")
public Object[] myFactory() {
    return new Object [] {new RegressionFileOne(param), new RegressionFileTwo(param)};
}

@Factory methods do not take groups though, so that is not possible. I've tried using TestNG objects and generating an xml file to run, however with xml I am limited to only sending Strings as parameters for the constructor.

I'm currently using TestNG 6.8.8

Are there updates in newer versions of TestNG to make this possible? Or is there some workaround? Thanks.

Upvotes: 0

Views: 570

Answers (1)

Krishnan Mahadevan
Krishnan Mahadevan

Reputation: 14746

This is not possible in TestNG (Even in the latest released version of TestNG 6.14.2 the behavior is the same).

@Factory annotation is mainly used to control test class instantiation. Test class can essentially be visualized as a container that houses one or more @Test annotated test methods.

groups is one of the way of filtering @Test methods to let TestNG figure out what to execute and what not to.

So first the test classes have to be instantiated before filtering can be applied. TestNG either instantiates the test classes on its own via the default constructor, or it relies on you to specify the parameter injection mechanism for the parameterized constructor via a data provider (or) by relying on you to actually invoke the parameterized constructor through a @Factory annotation.

Only after the test class instance is created, does TestNG get to the part of filtering which test methods to be executed.

So groups are not applicable for @Factory annotation at all.

Hope that adds clarity.

Upvotes: 1

Related Questions