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?
Goal: To Run TestNG using Command Prompt: Write a Java program. Convert the Java Program into TestNG. Open command prompt. Run the TestNG using command prompt.
Run the test by right click on the TestNG XML file and select Run As => TestNG Suite. Once the testng. xml file has run, we can see the results in the console.
To execute commands via the java command, we need to add a -classpath option: $ java -cp "~/. m2/repository/org/testng/testng/7.4.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With