How to run standalone TestNG project from jar/bat/
Asked Answered
E

3

10

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

Extraction answered 15/7, 2013 at 14:59 Comment(2)
Did you find any solution? I am stuck here too.Parasympathetic
Are you using 'Eclipse'? It is possible to export a 'launch' file that will execute your tests for you but you'll still need a single 'Suite' file that lists all the classes that need to be executed. The 'launch' file can then be executed from the command line.Snooty
P
5

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.

Parasympathetic answered 26/7, 2013 at 6:32 Comment(2)
Ah, right click on the package and pick this in the menu. Care to provide a more universal solution, rather than one that only happens to work on an unspecified editor? Or perhaps limit it to a specific editor and version, like "on this version of Eclipse" or "with IntelliJ 13" or "using Netbeans 8", or "Inside JDeveloper 12"... Otherwise your information is not that valuable. Those menus are not visible on my current editor. Maybe it was there in ages past but moved. Or maybe your menus were for a different editor.Stomachache
I have edited my response to include the IDE name and version. I don't have an universal solution though.Parasympathetic
J
2

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://mcmap.net/q/1167255/-how-to-run-testng-tests-from-main-in-an-executable-jar was very intriguing, I could not figure out how to make that work.

Jasen answered 6/8, 2013 at 3:45 Comment(0)
T
0

Here is the better way to do it.

You can just create a main method which will have list of all test classes to be executed as follows:

public static void main(String[] args) {
   TestListenerAdapter tla = new TestListenerAdapter();
   TestNG testng = new TestNG();
   testng.setTestClasses(new Class[] { test_start.class });
   testng.addListener(tla);
   testng.run();
}

Here is the reference URL from the official testng website.

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

Transducer answered 8/3, 2017 at 7:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.