I am using Selenium RC with Java using TestNG as Test Framework. I'm using Eclipse as IDE. I want to invoke TestNG from my own program very easily. How can I do that?
3 Answers
My following code in java works nicely:
@Test
public void testTestNGProgramatically(){
TestListenerAdapter tla = new TestListenerAdapter();
TestNG testng = new TestNG();
testng.setTestClasses(new Class[] {LoginAuthentication.class, GmailSigninSignout.class});
testng.addListener(tla);
testng.run();
}
You can get the details explanation by visiting the following URL:
http://testng.org/doc/documentation-main.html#running-testng-programmatically

- 36,924
- 42
- 155
- 176
-
In my above code: TestNG is deprecated in line "TestNG testng = new TestNG();" . What is the alternative of TestNG class? – Ripon Al Wasim Aug 30 '12 at 12:42
-
2Use `org.testng.TestNG`, not `com.beust.TestNG` (the one you are probably importing). – Cedric Beust Aug 30 '12 at 16:12
-
@CedricBeust: Thanks a lot. Yes, I have imported org.testng.TestNG instead of com.beust.TestNG and OK now – Ripon Al Wasim Aug 31 '12 at 03:43
-
above code executes multiple classes parallel, how to avoid [parallel execution? – sameer joshi Feb 10 '16 at 04:48
TheStijn gives a few good directions, although TestMethodWorker() is internal so you shouldn't use it.
Based on the question, I'm not even sure the original poster is trying to launch TestNG in a separate process, so the API documentation might be what you're looking for:
http://testng.org/doc/documentation-main.html#running-testng-programmatically

- 15,480
- 2
- 55
- 55
-
My following code is worked nicely: TestListenerAdapter tla = new TestListenerAdapter(); TestNG testng = new TestNG(); testng.setTestClasses(new Class[] {LoginAuthentication.class}); testng.addListener(tla); testng.run(); com.beust.testng.TestNG; TestNG is deprecated. Is there any alternative way? – Ripon Al Wasim Jul 02 '12 at 11:44
-
-
you can follow this blog as well http://dharshanaw.blogspot.com/2012/10/how-to-execute-testng-tests.html – Dharshana Nov 30 '12 at 07:06
-
I tried the snippet however I get a NoClassDefFoundError while trying to run it , do I have to do anything except cretaing my test class and a test method in it . – user2062360 Dec 08 '14 at 07:49
Have a look at org.testng.remote.RemoteTestNG, this will however require you to write an xml suite for your tests like:
<suite name="Default suite">
<test verbose="2" name="Default test">
<classes>
<class name="com...service.UserServiceImplTest"/>
</classes>
</test>
</suite>
Another entry point might be new org.testng.internal.TestMethodWorker(...).run() but you'll have to look at the code to determine the constructor args you need to set.
Perhaps other, more convenient entry points are available depending on your needs; I suggest to launch some test in debug mode, put a breakpoint in your test and go down the stack.

- 15,454
- 8
- 66
- 101
-
Yes, this is the content of xml file. So, build.xml file is run by using either eclipse or Ant – Ripon Al Wasim Jul 02 '12 at 04:20