Hi,
I tried an alternate approach.
package
org.automation.bo;
public class TestNGObjectExample {
@BeforeSuite(alwaysRun=true)
public static void setUpEnv(){
TestNG myTestNG=new TestNG();
TestListenerExample tle=new TestListenerExample();
myTestNG.addListener(tle);
myTestNG.addMethodSelector("deshaw.automation.bo.TestNGMethodSelector", 2);
myTestNG.setTestClasses( new Class[] {
TestDependencyExample.class,
TestLazyDataProviderExample.class,
TestNGExample.class,
});
myTestNG.run();
}
}
/*TestNGMethodSelecor class */
package
org.automation.bo;
public class TestNGMethodSelector implements IMethodSelector{
public boolean includeMethod(IMethodSelectorContext context,
ITestNGMethod method, boolean isTestMethod) {
boolean isIncluded=false;
if(!isTestMethod)
isIncluded=true;
else{
/*method present in TestDependencyExample.class */
if(method.getMethod().getName().equals("dependency"))
isIncluded=true;
/*method present in TestDependencyExample.class */
else if(method.getMethod().getName().equals("testDependsMethod"))
isIncluded=true;
/*method present in TestNGExample.class */
else if(method.getMethod().getName().equals("testMethod1"))
isIncluded=true;
else
isIncluded=false;
}
context.setStopped(true);
return isIncluded;
}
public void setTestMethods(List<ITestNGMethod> testMethods) {
}
}
In this approach also, TestNG engine first runs the methods selected in the method selector. After executing these test methods, TestNG again runs all the tests present in the classes in
org.automation.bo package.
For more clarity i'm providing the output also:
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running TestSuite
Entry point
[Parser] Running:
Command line suite
/*
* these are the methods selected in the method -selectors
*
Executing method
dependencyExecuting method
testDependsMethodExecuting
test method 1
-------------------------------------------
===============================================
Command line suite
Total tests run: 3, Failures: 0, Skips: 0
===============================================
/*
* TestNG runs the entire test suite again
*/
[XmlMethodSelector] CLASSNAME:deshaw.automation.bo.TestNGMethodSelector
[XmlMethodSelector] SETTING PRIORITY:2
[XmlMethodSelector] CLASSNAME:deshaw.automation.bo.TestNGMethodSelector
[XmlMethodSelector] SETTING PRIORITY:2
[XmlMethodSelector] CLASSNAME:deshaw.automation.bo.TestNGMethodSelector
[XmlMethodSelector] SETTING PRIORITY:2
[XmlMethodSelector] CLASSNAME:deshaw.automation.bo.TestNGMethodSelector
[XmlMethodSelector] SETTING PRIORITY:2
[XmlMethodSelector] CLASSNAME:deshaw.automation.bo.TestNGMethodSelector
[XmlMethodSelector] SETTING PRIORITY:2
Executing method dependency
Executing method testDependsMethod
Executing testIterator example
Executing test method 2
Executing test method 1
Executing test method 3
Tests run: 6, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.891 sec
Any inputs on how to resolve this issue or any alternatives will be much appreciated.
Thanks,
Prasanna