Running TestNG programmatically

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Running TestNG programmatically


We're running many Selenium tests through TestNG and I need to run
them in parallel so that I can save time with our test execution.  Up
until now, I have been running the Selenium tests either through the
Eclipse TestNG plug-in or through an Ant task which utilizes our
testng.xml file.  If I utilize TestNG's parallel = methods attribute,
I don't have control over how the Selenium tests are multithreaded.  
However, if I am able to run the tests programmatically, I can utilize
a multi-threading solution (with some java.util.concurrent help) that
should be able to solve my problem and enable me to have more control
over which method gets executed in which thread and which method will
open up a new Selenium object, etc.

The TestNG documentation online (
main.html#running-testng-programmatically) makes running TestNG
programmatically sound like it should be very easy but every time I do
exactly how they describe it should be, I get either a NPE or a
different error (like it can't find the test classes).  Has anyone
else had this difficulty or is it something dealing with my classpath
and the test classes?

So far, I've had to create a number of Java objects to identify the
correct TestNG classes (see below for code).  Obviously, I'm not
finished since I'm still playing with trying to get the correct
classes/groups/methods.  Must I be doing this?  Something tells me I'm
doing way more work than I really need to.  If someone were to please
offer me some assistance and point me in the right direction, I'd
appreciate it.

 XmlSuite xmlSuite = new XmlSuite();
 XmlTest test = new XmlTest(xmlSuite);
 List<String> groups = new ArrayList<String>();
 List<XmlPackage> packages = new ArrayList<XmlPackage>();
 List<String> testPackage = new ArrayList<String>();
 XmlPackage xmlpackage = new XmlPackage();


TestListenerAdapter[] testListeners = new TestListenerAdapter[]{tla};
SuiteRunner.DefaultTestRunnerFactory suiteRunner =
SuiteRunner.DefaultTestRunnerFactory(testListeners, false);
SeleniumSuite seleniumSuite = new SeleniumSuite();
TestRunner testRunner = suiteRunner.newTestRunner(seleniumSuite,
SeleniumAnnotationFinder annotationFinder = new
HashMap<Class, List<Object>> instanceMap = new HashMap<Class,
TestNGClassFinder finder = new TestNGClassFinder(testClasses,
                instanceMap, test, annotationFinder);
IClass[] iClasses = finder.findTestClasses();
for (IClass iClass : iClasses) {
                Class clazz = iClass.getClass();
                TestClass tc = null;
                tc = new TestClass(iClass, tc);
                Class[] testClassArr = tc.getTestClasses();
                ITestNGMethod[] beforeSuiteMethods =
                ITestNGMethod[] beforeTestMethods =
                ITestNGMethod[] allTestMethods = tc.getTestMethods();
                ITestMethodFinder methodFinder =
                XmlMethodSelector xmlSelector = new

You received this message because you are subscribed to the Google Groups "testng-users" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at