How to run TestNG from command line
Asked Answered
W

14

40

How exactly do I run a .java TestNG project from a command line?

I have read through the TestNG documentation, and tried the following to no avail:

C:\projectfred> java org.testng.TestNG testng.xml 

... with the following testng.xml file in my project:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >  
<suite name="SuiteAll" verbose="1">
    <test name="TestAll">  
        <packages>  
            <package name="com.project.fred.tests"/>
        </packages>  
    </test>  
</suite>

The error I get is this:

Exception in thread "main" java.lang.NoClassDefFoundError: org/testng/TestNG
Caused by: java.lang.ClassNotFoundException: org.testng.TestNG
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: org.testng.TestNG.  Program will exit.

Obviously, I am not referencing TestNG correctly in my command line. Does anyone know how to get this working?

Wort answered 10/8, 2012 at 6:51 Comment(0)
W
27

You need to have the testng.jar under classpath.

try C:\projectfred> java -cp "path-tojar/testng.jar:path_to_yourtest_classes" org.testng.TestNG testng.xml

Update:

Under linux I ran this command and it would be some thing similar on Windows either

test/bin# java -cp ".:../lib/*" org.testng.TestNG testng.xml

Directory structure:

/bin - All my test packages are under bin including testng.xml
/src - All source files are under src
/lib - All libraries required for the execution of tests are under this.

Once I compile all sources they go under bin directory. So, in the classpath I need to specify contents of bin directory and all the libraries like testng.xml, loggers etc over here. Also copy testng.xml to bin folder if you dont want to specify the full path where the testng.xml is available.

 /bin
    -- testng.xml
    -- testclasses
    -- Properties files if any.
 /lib
    -- testng.jar
    -- log4j.jar

Update:

Go to the folder MyProject and type run the java command like the way shown below:-

java -cp ".: C:\Program Files\jbdevstudio4\studio\plugins\*" org.testng.TestNG testng.xml

I believe the testng.xml file is under C:\Users\me\workspace\MyProject if not please give the full path for testng.xml file

Wortham answered 10/8, 2012 at 7:46 Comment(11)
Thanks. Can you give an example with exact paths? I've tried something like C:\projectfred> java -cp "C:\path-tojar\testng.jar:C:\path_to_yourtest_classes" org.testng.TestNG testng.xml , but as you can see there are lots of colons in that and it throws an error.Wort
can you please give me the hierarchy where your test classes and related libs are available?Wortham
(Windows 7 machine) Test classes: C:\Users\me\workspace\MyProject (this is where all my TestNG .java files are). Related libs: C:\Program Files\jbdevstudio4\studio\plugins . Is that what you were after?Wort
Yes I tried, thanks very much for your efforts. I get the same error as before, NoClassDefFoundError for org.testng.TestNG . I am using the TestNG plugin for Eclipse, does that matter?Wort
This executes something, but runs 0 tests: java -cp "." -jar "C:\Program Files\jbdevstudio4\studio\plugins\org.testng.eclipse_5.14.6.20110119_1050\lib\testng.jar" testng.xmlWort
In this case, you dont have your test classes under classpath. Are you under the same directory where your class files exists? as you using . which means current directory only,so I suggest you to use the complete path to the folder where your class files are available,if you are not in the directory of your class files. FYI, I was able to run all tests perfectly on my machine with the command I have mentioned in the answers. If you are not able to run then please check the class pathWortham
I guess my incompetence shines through here :S. I'm going to try and find a way to run TestNG tests as either an Ant task or by some other means on Jenkins. Here is my latest post: #16782914Wort
Finally got this working with an ant script, which I will post shortly.Wort
I am getting this error,when i run testng from the terminal,what could be the possible reasons for the issue. TestingSuite Total tests run: 0, Failures: 0, Skips: 0 Below is my command,i am using ubuntu machine. java -cp "testng jar file path:path of class files" org.testng.TestNG testng.xml java -cp "/home/user/Documents/eclipse/plugins/org.testng.eclipse_6.11.0.201703011520/lib/*:/home/user/Documents/APPIUMWorkSpace/EOneMobile/target/classes/" org.testng.TestNG /home/user/Documents/APPIUMWorkSpace/EOneMobile/testng.xmlOffenseless
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "testng.org/testng-1.0.dtd"> <suite name="TestingSuite" parallel="none" verbose="1"> <test name="Test1"> <classes> <class name="testcases.TC002_Loginnative"/> </classes> </test> </suite>Offenseless
You separated the two directories by a colon, but it should be separated by a semi-colon for the CLASSPATH environment in windowsKele
M
16

If you are using Maven, you can run it from the cmd line really easy, cd into the directory with the testng.xml (or whatever yours is called, the xml that has all the classes that will run) and run this cmd:

mvn clean test -DsuiteXmlFile=testng.xml 

This page explains it in much more detail: How to run testng.xml from Maven command line

I didn't know it mattered if you were using Maven or not so I didn't include it in my search terms, I thought I would mention it here in case others are in the same situation as I was.

Meurer answered 27/4, 2019 at 0:22 Comment(0)
O
7

Ok after 2 days of trying to figure out why I couldn't run the example from

http://www.tutorialspoint.com/testng/testng_environment.htm the following code did not work for me

C:\TestNG_WORKSPACE>java -cp "C:\TestNG_WORKSPACE" org.testng.TestNG testng.xml

The fix for it is as follows: I came to the following conclusion: First off install eclipse, and download the TestNG plugin. After that follow the instructions from the tutorial to create and compile the test class from cmd using javac, and add the testng.xml. To run the testng.xml on windows 10 cmd run the following line:

java -cp C:\Users\Lenovo\Desktop\eclipse\plugins\org.testng.eclipse_6.9.12.201607091356\lib\*;C:\Test org.testng.TestNG testng.xml

to clarify: C:\Users\Lenovo\Desktop\eclipse\plugins\org.testng.eclipse_6.9.12.201607091356\lib\* The path above represents the location of jcommander.jar and testng.jar that you downloaded by installing the TESTNG plugin for eclipse. The path may vary so to be sure just open the installation location of eclipse, go to plugins and search for jcommander.jar. Then copy that location and after that add * to select all the necessary .jars.

C:\Test  

The path above represents the location of the testing.xml in your project. After getting all the necessary paths, append them by using ";".

I hope I have been helpful to some of you guys :)

Orlop answered 7/9, 2016 at 8:49 Comment(0)
W
6

Thanks a lot it was really very helpful.. I was able to run my TestNG classes using command line, but still I would like to wrap-up the things one last time as follows..

1) Set the classpath for the testng.jar in the environment variables
(either manually or via command line).

2) Create a new folder "MyTest".

3) Compile all your classes(testNG+java) and put them(.class files )
in a new folder "Mytest\bin".

4) Put your 'testng.xml' and '.properties' files to "Mytest\bin"
again.

5) Put all the .jar files (testng,selenium... etc) in a new folder
"Mytest\lib".

6) Browse to the folder "bin" via command line and run the command
java -cp "..\lib\*;" org.testng.TestNG testng.xml

this should run your testNG cases.

Weber answered 2/12, 2014 at 9:44 Comment(0)
W
3

Using " ; " as delimiter on windows issue got resolved.

java -cp "path of class files; testng jar file path" org.testng.TestNG testng.xml

ex:-

java -cp ".\bin;..\common_lib\*;" org.testng.TestNG testng.xml
Wert answered 3/10, 2014 at 17:51 Comment(0)
R
3

Prepare MANIFEST.MF file with the following content

Manifest-Version: 1.0
Main-Class: org.testng.TestNG

Pack all test and dependency classes in the same jar, say tests.jar

jar cmf MANIFEST.MF tests.jar -C folder-with-classes/ .

Notice trailing ".", replace folder-with-classes/ with proper folder name or path.

Create testng.xml with content like below

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Tests" verbose="5">
  <test name="Test1">
    <classes>
      <class name="com.example.yourcompany.qa.Test1"/>
    </classes>
  </test>  
</suite>

Replace com.example.yourcompany.qa.Test1 with path to your Test class.

Run your tests

java -jar tests.jar testng.xml
Roscoe answered 28/9, 2016 at 14:42 Comment(0)
E
3

I had faced same problem because I downloaded TestNG plugin from Eclipse. Here what I did to get the Job done :

  1. After adding TestNG to your project library create one folder in your Project names as lib ( name can be anything ) :

  2. Go to "C:\Program Files\Eclipse\eclipse-java-mars-R-win32-x86_64\eclipse\plugins" location and copy com.beust.jcommander_1.72.0.jar and org.testng_6.14.2.r20180216145.jar file to created folder (lib).

Note : Files are testng.jar and jcommander.jar

  1. Now Launch CMD, and navigate to your project directory and then type :
    Java -cp C:\Users\User123\TestNG\lib*;C:\Users\User123\TestNG\bin org.testng.TestNG testng.xml

That's it !
Let me know if you have any more concerns.

Electrothermics answered 5/4, 2018 at 13:14 Comment(2)
I'm doing exactly what you've proposed here, but I'm getting an error Error: Could not find or load main class java Caused by: java.lang.ClassNotFoundException: java Please help!Spathose
Do project clean from project menu using eclipse top menu bar.Electrothermics
L
3

Below command works for me. Provided that all required jars including testng jar kept inside lib.

java -cp "lib/*" org.testng.TestNG testng.xml
Lethe answered 8/6, 2018 at 9:46 Comment(0)
I
1

You will need to use semicolon as delimiter while specifying the jar and class file path in windows. This solved the issue.
Assuming the class file is under C:.

java -cp ".;C:\testng.jar" org.testng.TestNG testing.xml
Ingvar answered 1/9, 2014 at 10:45 Comment(0)
S
1

After gone throug the various post, this worked fine for me doing on IntelliJ Idea:

java -cp "./lib/*;Path to your test.class"  org.testng.TestNG testng.xml

Here is my directory structure:

/lib
  -- all jar including testng.jar
/out
  --/production/Example1/test.class
/src
 -- test.java
testing.xml

So execute by this command:

java -cp "./lib/*;C:\Users\xyz\IdeaProjects\Example1\out\production\Example1" org.testng.TestNG testng.xml

My project directory Example1 is in the path:

C:\Users\xyz\IdeaProjects\
Saez answered 9/4, 2019 at 4:37 Comment(0)
F
1

If none of the above answers work, you can run the test in IDE, get the class path and use it in your command. Ex: If you are using Intellij IDEA, you can find it at the top of the console(screenshot below). Intellij Console

Clicking on the highlighted part expands and displays the complete class path.

you need to remove the references to jars inside the folder: JetBrains\IntelliJ IDEA Community Edition 2019.1.3

java -cp "path_copied" org.testng.TestNG testng.xml

Fidelfidela answered 18/6, 2019 at 19:44 Comment(0)
S
0

I tried everything on this post but it turns out I was missing a jar file. Here's my solution:

Add guice-4.2.2.jar to your jar files if you are getting the following error. If you are using maven then add the following dependency.

<dependency>
    <groupId>com.google.inject</groupId>
    <artifactId>guice</artifactId>
    <version>4.1.0</version>
</dependency>


Exception in thread "main" java.lang.NoClassDefFoundError: com/google/inject/Stage
        at org.testng.internal.Configuration.<init>(Configuration.java:33)
        at org.testng.TestNG.init(TestNG.java:216)
        at org.testng.TestNG.<init>(TestNG.java:200)
        at org.testng.TestNG.privateMain(TestNG.java:1312)
        at org.testng.TestNG.main(TestNG.java:1304)
Caused by: java.lang.ClassNotFoundException: com.google.inject.Stage
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
        ... 5 more
Spathose answered 6/11, 2020 at 18:52 Comment(0)
E
0

Try below command

java -cp bin\;jars\* org.testng.TestNG testng.xml

where bin has the compiled classes of your projects and jars folder contains the necessary jars to execute your testng.xml file

Make sure not to use bin\* instead of bin\ as it will lead you to errors.

Now necessary jars will be your TestNG jar, jcommander jar, and the bsh jar, you can download these jars from here

Your project structure would be something like below -

Project
    |_ _ bin
      | _ _ your compiled classes
    |_ _ jars
      |_ _ testng.jar
      |_ _ jcommander.jar
      |_ _ bsh.jar
    |_ _ testng.xml

I have also written an article which explains how to run a testng.xml file while making a new project or in an existing project. Hope you find your solution in the article.

Echevarria answered 25/4, 2021 at 6:0 Comment(0)
P
0

Add this plugin if your project pom.xml

 <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19.1</version>
            <configuration>
                <reportsDirectory>./test-output</reportsDirectory>
                <suiteXmlFiles>
                    <!-- pass testng.xml files as argument from command line -->
                    <suiteXmlFile>${suiteXmlFile}</suiteXmlFile>
                </suiteXmlFiles>
            </configuration>
        </plugin>

after that use following command to pass the testng xml location:

mvn clean test -Dsurefire.suiteXmlFiles=/path/to/testng.xml

testng.xml should include details about test classes or packages to run. Here is an example of content in testng.xml file:

enter image description here

You can follow this link to create one https://www.softwaretestinghelp.com/testng-example-to-create-testng-xml/

Philina answered 14/8, 2022 at 21:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.