I would like to know if there is a way of having different configuration methods (let\'s say at class level, @Before/AfterClass) that will enable the user to select which co
Depending on your test setup you might just use groups:
public class Test {
@BeforeClass(groups = {"A"})
public void configuration1() {...}
@BeforeClass(groups = {"B"})
public void configuration2() {...}
@Test(groups = {"A", "B"})
public void testFoo() {...}
@Test(groups = {"A"})
public void testFoo() {...}
}
Or you might think about parameterizing your tests/use data providers.
TestNG provides Listeners to customize default functionality. For your requirement, IInvokedMethodListener needs to be implemented. I don't think command line arguments can be passed to a testng xml but try your luck. In below code, "configuration" is assumed to be a parameter from testng xml.
Sample code(not tested)
public class CustomListener implements IInvokedMethodListener {
@Override
public void beforeInvocation(IInvokedMethod method, ITestResult itr) {
if (method.isConfigurationMethod()) {
String userPassed = method.getTestMethod().getXmlTest()
.getLocalParameters().get("configuration");
if(based on userPassed , call configuration() method){
}
}
}
@Override
public void afterInvocation(IInvokedMethod arg0, ITestResult arg1) {
// TODO Auto-generated method stub
}
}
If you decide to call this Listener from your java class, then you can read the cmd line args and pass it to the CustomListener. Just an after thought.