Running TestNG programmatically

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

Running TestNG programmatically

Adam66

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 (http://testng.org/doc/documentation-
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();
 xmlSuite.setName(testGroup);
 XmlTest test = new XmlTest(xmlSuite);
 test.setName(testGroup);
 List<String> groups = new ArrayList<String>();
 List<XmlPackage> packages = new ArrayList<XmlPackage>();
 List<String> testPackage = new ArrayList<String>();
 testPackage.add("selenium");
 XmlPackage xmlpackage = new XmlPackage();
 xmlpackage.setName(testGroup);
 xmlpackage.setInclude(testPackage);

 groups.add("system_config");
 groups.add(testGroup);
 packages.add(xmlpackage);
 test.setIncludedGroups(groups);
 test.setXmlPackages(packages);

TestListenerAdapter[] testListeners = new TestListenerAdapter[]{tla};
SuiteRunner.DefaultTestRunnerFactory suiteRunner =
                new
SuiteRunner.DefaultTestRunnerFactory(testListeners, false);
SeleniumSuite seleniumSuite = new SeleniumSuite();
TestRunner testRunner = suiteRunner.newTestRunner(seleniumSuite,
test);
testRunner.addTestListener(tla);
testRunner.setVerbose(10);
SeleniumAnnotationFinder annotationFinder = new
SeleniumAnnotationFinder();
HashMap<Class, List<Object>> instanceMap = new HashMap<Class,
List<Object>>();
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 =
tc.getBeforeSuiteMethods();
                ITestNGMethod[] beforeTestMethods =
tc.getBeforeTestMethods();
                ITestNGMethod[] allTestMethods = tc.getTestMethods();
                ITestMethodFinder methodFinder =
tc.getTestMethodFinder();
                XmlMethodSelector xmlSelector = new
XmlMethodSelector();
}


--~--~---------~--~----~------------~-------~--~----~
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 http://groups.google.com/group/testng-users?hl=en
-~----------~----~----~----~------~----~------~--~---