0

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

  • Open CMD
  • CD to the folder containing the test.xml file
  • Syntax: java -cp "[complete path to testNG jar]:[complete path to the compiled java classes to run]" org.testng.TestNG [testng xml file name]
  • Actual CMD input I entered: 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

  • Set environment variable: TESTNG_HOME = C:\Users\johndoe\Documents\My Docs\03_OE\Java\workspace\Maven\m2\repository\org\testng\testng\6.9.10
  • Set environment variable: CLASSPATH = %CLASSPATH%;%TESTNG_HOME%\testng-6.9.10.jar
  • CD to the folder containing the test.xml file
  • Syntax: java -cp "[complete path to the compiled java classes to run]" org.testng.TestNG [testng xml file name]
  • Actual input: 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

  • CD to the folder containing the test.xml file
  • Enter: 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

  • Download a fresh testNG jar file. Results still same.
  • Add jcommander.jar in classpath: 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
    

  • Seeing the error above, I added the compiled java classes path in classpath: 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!

  • iamkenos
    • 1,446
    • 8
    • 24
    • 49
    • Possible duplicate of [How to run TestNG from command line](http://stackoverflow.com/questions/11896791/how-to-run-testng-from-command-line) – juherr Jan 12 '17 at 08:51
    • I referenced that URL on my post as one of the methods I tried but was not working. (Method 1, source 2.) Sadly, I can't post more than 2 links because of lacking reps so I had to put it as a plain text. – iamkenos Jan 12 '17 at 10:15

    2 Answers2

    1

    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
    
    juherr
    • 5,640
    • 1
    • 21
    • 63
    • Thanks for the tip! I removed the additional "\" just right before org.testng.TestNG... and it produced a different result but I think i'm getting closer. I'm editing the post for details. Thanks! – iamkenos Jan 12 '17 at 10:11
    • @Kenos Run the commande from `C:\Users\johndoe\Documents\My Docs\03_OE\Java\workspace\KeywordDrivenTool\JavaTestNBS\target\test-classes\` – juherr Jan 12 '17 at 10:34
    • Tried doing so but I'm getting a `FileNotFoundException...` `[TestNG] [ERROR] No test suite found. Nothing to run`. My testng xml file is under `C:\Users\johndoe\Documents\My Docs\03_OE\Java\workspace\KeywordDrivenTool\JavaTestNBS\target\test-classes\testngRunner` – iamkenos Jan 13 '17 at 01:37
    • Update: Voting this as the correct answer. There seems to be a problem with my test classes. Those classes are referencing another (compiled) class in a project-referenced jar for common functions (like launching the webdriver etc..). I tried testing with a simple class without any dependencies and it worked. I have to look into that issue separately. Thanks! – iamkenos Jan 13 '17 at 06:01
    • I put testng.xml, bin, lib and src folder in directory I kept all selenium jars under lib directory, SampleNavigation.class under bin directory, SampleNavigation.java under src Getting an error with below command: java -cp /tmp/seltest/bin;/tmp/seltest/lib/* org.testng.TestNG testng.xml /tmp/seltest is the directory is my root directory – aniruddha Feb 01 '18 at 10:16
    1

    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:

    1. Export jar file in eclipse.File->Export. note the red box

    2. 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.

    1. set CLASSPATH=%NGLIB%*;%JAVA_CLIENT%\java-client-5.0.0-BETA1.jar;[your exported project jar];
    2. java org.testng.TestNG testMainXML.xml

    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"

    ssm
    • 21
    • 5
    • How did you create .jar file of your project. I thought you can only create .jar file of java classes where we have main() method that drives everything. Here we are using testNG annotation. – zara Nov 12 '20 at 18:37