How to run TestNG from command line

141,214

Solution 1

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

Solution 2

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.

Solution 3

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 :)

Solution 4

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.

Solution 5

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
Share:
141,214
Fuzzy Analysis
Author by

Fuzzy Analysis

I can use Google Web Search.

Updated on April 25, 2021

Comments

  • Fuzzy Analysis
    Fuzzy Analysis about 3 years

    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?

  • Fuzzy Analysis
    Fuzzy Analysis over 11 years
    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.
  • Patton
    Patton over 11 years
    can you please give me the hierarchy where your test classes and related libs are available?
  • Fuzzy Analysis
    Fuzzy Analysis over 11 years
    (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?
  • Fuzzy Analysis
    Fuzzy Analysis over 11 years
    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?
  • Fuzzy Analysis
    Fuzzy Analysis over 11 years
    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\te‌​stng.jar" testng.xml
  • Patton
    Patton over 11 years
    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 path
  • Fuzzy Analysis
    Fuzzy Analysis almost 11 years
    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: stackoverflow.com/questions/16782414/…
  • Fuzzy Analysis
    Fuzzy Analysis almost 11 years
    Finally got this working with an ant script, which I will post shortly.
  • user3511026
    user3511026 over 6 years
    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.1‌​1.0.201703011520/lib‌​/*:/home/user/Docume‌​nts/APPIUMWorkSpace/‌​EOneMobile/target/cl‌​asses/" org.testng.TestNG /home/user/Documents/APPIUMWorkSpace/EOneMobile/testng.xml
  • user3511026
    user3511026 over 6 years
    <?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>
  • Conor
    Conor about 4 years
    You separated the two directories by a colon, but it should be separated by a semi-colon for the CLASSPATH environment in windows
  • zara
    zara over 3 years
    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!
  • cruisepandey
    cruisepandey over 3 years
    Do project clean from project menu using eclipse top menu bar.