Reputation: 1566
Some notes:
1. The TestNG jar I'm using initially is from the maven repo within our java project. All the methods below are tested with both the maven testNG jar and a fresh testNG 6.9.12 download.
2. I can run the file just file from within eclipse. Only having problems when I try to run from command line.
3. The ultimate goal is to trigger the command line to run the test via a javafx application button press. I'd appreciate it greatly if the solutions provided avoid typing absolute paths in cmd as the application is to be used on different machines, but as long as I can run it on my machine, I'll try to figure out the rest separately :)
4. From what I have seen online there are different problems encountered when trying to run a testng xml file. Please bear with me if this stack question follows a series of other questions related to running the file. Thanks in advance.
Problem:
I'm trying to run a testNG "test.xml" using command line.
There are numerous guide over the web but none is working for me.
Here are some of the things I've tried so far, among a couple of others:
Method 1
Sources:
1. https://www.safaribooksonline.com/library/view/testng-beginners-guide/9781782166009/ch02s05.html
2. http://stackoverflow.com/questions/11896791/how-to-run-testng-from-command-line
java -cp "[complete path to testNG jar]:[complete path to the compiled java classes to run]" org.testng.TestNG [testng xml file name]
java -cp "C:\Users\johndoe\Documents\My Docs\03_OE\Java\workspace\Maven\m2\repository\org\testng\testng\6.9.10\testng-6.9.10.jar:C:\Users\johndoe\Documents\My Docs\03_OE\Java\workspace\KeywordDrivenTool\JavaTestNBS\target\test-classes\NBS\testcases" org.testng.TestNG test.xml
Result:
Error: Could not find or load main class org.testng.TestNG
I also tried replacing the [complete path to testNG jar]
with "." like java -cp ".:C:\Users\johndoe\Documents\My Docs\03_OE\Java\workspace\KeywordDrivenTool\JavaTestNBS\target\test-classes\NBS\testcases" org.testng.TestNG test.xml
as suggested on the 2nd source link above and got the same error.
Method 2
Source:
1. https://www.tutorialspoint.com/testng/testng_environment.htm
TESTNG_HOME = C:\Users\johndoe\Documents\My Docs\03_OE\Java\workspace\Maven\m2\repository\org\testng\testng\6.9.10
CLASSPATH = %CLASSPATH%;%TESTNG_HOME%\testng-6.9.10.jar
java -cp "[complete path to the compiled java classes to run]" org.testng.TestNG [testng xml file name]
java -cp "C:\Users\johndoe\Documents\My Docs\03_OE\Java\workspace\KeywordDrivenTool\JavaTestNBS\target\test-classes\NBS\testcases" org.testng.TestNG test.xml
Result:
Error: Could not find or load main class org.testng.TestNG
Method 3
Source:
1. http://stackoverflow.com/questions/28303723/could-not-find-or-load-main-class-org-testng-testng
java -cp ".;C:\Users\johndoe\Documents\My Docs\03_OE\Java\workspace\Maven\m2\repository\org\testng\testng\6.9.10\testng-6.9.10.jar" org.testng.TestNG test.xml
Result:
java.lang.NoClassDefFoundError: com/beust/jcommander/
ParameterException
Then I tried to troubleshoot method 3 based on comments from http://stackoverflow.com/questions/7100821/suddenly-cant-run-testng-tests-from-ant-testng-caused-by-java-lang-classnot
java -cp ".;C:\Users\johndoe\Documents\My Docs\03_OE\Java\workspace\Maven\m2\repository\org\testng\testng\6.9.10\testng-6.9.10.jar;C:\Users\johndoe\Documents\My Docs\03_OE\Java\workspace\Maven\m2\repository\com\beust\jcommander\1.48\jcommander-1.48.jar" org.testng.TestNG test.xml
Result:
[TestNG] [ERROR]
Cannot find class in classpath: NBS.testcases.LoginWithBayanUser
java -cp ".;C:\Users\johndoe\Documents\My Docs\03_OE\Java\workspace\Maven\m2\repository\org\testng\testng\6.9.10\testng-6.9.10.jar;C:\Users\johndoe\Documents\My Docs\03_OE\Java\workspace\Maven\m2\repository\com\beust\jcommander\1.48\jcommander-1.48.jar;C:\Users\johndoe\Documents\My Docs\03_OE\Java\workspace\KeywordDrivenTool\JavaTestNBS\target\test-classes\NBS\testcases" org.testng.TestNG test.xml
Result:
[TestNG] [ERROR]
Cannot find class in classpath: NBS.testcases.LoginWithBayanUser
To summarize:
I think the closest attempt I got was after troubleshooting method 3 but still unable to run the xml. The same xml runs from within eclipse.
Command breakdown:
Full: java -cp ".;C:\Users\johndoe\Documents\My Docs\03_OE\Java\workspace\Maven\m2\repository\org\testng\testng\6.9.10\testng-6.9.10.jar;C:\Users\johndoe\Documents\My Docs\03_OE\Java\workspace\Maven\m2\repository\com\beust\jcommander\1.48\jcommander-1.48.jar;C:\Users\johndoe\Documents\My Docs\03_OE\Java\workspace\KeywordDrivenTool\JavaTestNBS\target\test-classes\NBS\testcases" org.testng.TestNG test.xml
1st part: java -cp
2nd part: "[.];[full path to testNG jar];
3rd part: "[full path to jcommander jar];
4th part: "[full path to compiled java classes]"
5th part: org.testng.TestNG [testNG xml file name]
Command executed on [directory of testNG xml file]
Please help! Thanks in advance!
=======================================================
EDIT 1:
I removed extra folders from the compiled java classes folder as suggested by Julien Herr and it produced a different result. I think I'm getting closer but still not able to run.
[TestNGClassFinder] Warning: Can't link and determine methods of class NBS.testcases.LoginWithBayanUser
[TestNGClassFinder] Warning: Can't link and determine methods of class NBS.testcases.LoginWithBayanUser2
[[TestNGClassFinder]] Unable to read methods on class NBS.testcases.LoginWithBayanUser - unable to resolve class reference Automation/framework/dataModel/CaseInfo
[[TestNGClassFinder]] Unable to read methods on class NBS.testcases.LoginWithBayanUser2 - unable to resolve class reference Automation/framework/dataModel/CaseInfo
[TestNG] Running:
C:\Users\johndoe\Documents\My Docs\03_OE\Java\workspace\KeywordDrivenTool\JavaTestNBS\target\test-classes\testngRunner\test.xml
===============================================
Suite1
Total tests run: 0, Failures: 0, Skips: 0
===============================================
Here's the contents of my test.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite1" verbose="1" parallel="classes" thread-count="2" preserve-order="true">
<test name="Regression suite 1" >
<parameter name="globalXlsDataIndex" value="1"/>
<classes>
<class name="NBS.testcases.LoginWithBayanUser" />
<class name="NBS.testcases.LoginWithBayanUser2" />
</classes>
</test>
</suite>
To reiterate, the same xml file runs from within eclipse.
Thanks!
=======================================================
EDIT 2:
I added C:\Users\johndoe\Documents\My Docs\03_OE\Java\workspace\KeywordDrivenTool\JavaTestNBS\lib\*
in the classpath where a compiled jar containing the Automation/framework/dataModel/CaseInfo
is located. Now I'm getting another error:
[TestNG] [ERROR]
Cannot instantiate class NBS.testcases.LoginWithBayanUser
Hope you can help! Thanks!
Upvotes: 0
Views: 11610
Reputation: 21
I have taken a whole afternoon to fix this problem.Here is my practice,maybe it'll help you:
The error:"Cannot find class in classpath".It's caused by error jar of your project.I follow the direction of others,always failed.When I use the eclipse to "Export" my project to a jar file.It works!
Summry:
Export jar file in eclipse.File->Export. note the red box
Set environment variable. set NG_LIB=[dir of your testng lib lies] set JAVA_CLIENT=[dir of your java-client.jar]--- My project depends on java-client.
And set other dependencies.
If you meet "Unable to read methods on class XXX",it means compiler found a denpendency that you didn't provide.You should follow the step 2,set the depengdency's dir,and add the dependency jar into step 3.meet "Unable to read methods on class XXX"
Upvotes: 1
Reputation: 5740
Try:
java -cp ".;C:\Users\johndoe\Documents\My Docs\03_OE\Java\workspace\Maven\m2\repository\org\testng\testng\6.9.10\testng-6.9.10.jar;C:\Users\johndoe\Documents\My Docs\03_OE\Java\workspace\Maven\m2\repository\com\beust\jcommander\1.48\jcommander-1.48.jar;C:\Users\johndoe\Documents\My Docs\03_OE\Java\workspace\KeywordDrivenTool\JavaTestNBS\target\test-classes" org.testng.TestNG test.xml
Upvotes: 1