How to run standalone TestNG project from jar/bat/

itin picture itin · Jul 15, 2013 · Viewed 11.1k times · Source

I have a TestNG project. Don't have any main class, currently it is running like "Run As TestNG".

I want to export it as runnable jar or jar so that any one can just hit a command from command line and test cases start running.

Could any one help me out in this? or suggest any other way to deliver the code in runnable form...

I am not using ant or maven.

Thanks

Answer

Janaaaa picture Janaaaa · Jul 26, 2013

I seem to have found the solution after a bit of googling. This works fine in Eclipse (Juno).

Say, you have a TestNG file named 'Tests.java'. As you rightly pointed out, there won't be a class with main method. So, we have to create a new Java class file under the same package. Let us name it 'MainOne.java'. This will have a class with main method.

Here is the code you need:

import com.beust.testng.TestNG;

public class MainOne {

    public static void main(String[] args) {
        TestNG testng = new TestNG();
         Class[] classes = new Class[]{Tests.class};
         testng.setTestClasses(classes);
         testng.run();

    }

Run the 'MainOne.java' as a Java application. Then right click on the package -> Export -> Runnable Jar [Choose 'MainOne' as Launch Configuration] -> Finish.