Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run standalone TestNG project from jar/bat/

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

like image 757
itin Avatar asked Jul 15 '13 14:07

itin


2 Answers

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.

like image 195
Janaaaa Avatar answered Oct 20 '22 12:10

Janaaaa


My current understanding is that, in order to benefit from the parallel niftiness of TestNG, one should use the static main method in org.testng's jar file when running the Java class from the command line rather than from inside Eclipse IDE.

The issue then becomes classpath, which defines how java finds all the JAR files. I found http://javarevisited.blogspot.com/2012/10/5-ways-to-add-multiple-jar-to-classpath-java.html to be most useful because it has the * wildcard mentioned --- VERY helpful when you need to reference all the jar files required for Selenum + TestNG + custom test suites.

This is my current Windows BAT file, and it works. ADV.jar contains my custom class but no main method.

setlocal
set r=d:\Apps\Selenium\
cd /d %~dp0
java -classpath %r%Downloaded\*;%r%MyCompany\ADV.jar; org.testng.TestNG .\testng-customsuite-adv.xml
pause

All the JAR files that I downloaded from public places went into my d:\Apps\Selenium\Downloaded folder. I put my custom ADV.jar file in d:\Apps\Selenium\MyCompany to keep it separate.

I created my ADV.jar file from Eclipse using Export Jar file and ignored warnings about a missing main method.

Aside: while this https://stackoverflow.com/a/16879386/424855 was very intriguing, I could not figure out how to make that work.

like image 27
user424855 Avatar answered Oct 20 '22 12:10

user424855