Invoking testng.xml through the command line (Classpath problem)

5,583 views
Skip to first unread message

Zack Lemley

unread,
May 24, 2011, 12:05:14 PM5/24/11
to testng-users
I'm attempting to run Selenium 2 tests using the TestNG framework
through the command line. I can run the tests through Eclipse by right
clicking on either the 3 browser test runners and doing 'Run As >
TestNG Test' or by right clicking on the 'testng.xml' and doing 'Run
As > TestNG Suite'.



My project consists of this structure:

C:\projects\SeleniumTest
\src
testng.xml
\tests
FirefoxTestRunner.java
InternetExplorerTestRunner.java
ChromeTestRunner.java
\utils
Tests.java
Helpers.java


The actual tests with the @Test annotation are in 'Tests.java,' which
as you can see below is never called explicitly. The 3 browser test
runners extend 'Tests,java'.




Here is my testng.xml file:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Selenium Test" verbose="10">

<test name="Tests" preserve-order="true">
<classes>
<class name="tests.FirefoxTestRunner"/>
<class name="tests.InternetExplorerTestRunner"/>
<class name="tests.ChromeTestRunner"/>
</classes>
</test>
</suite>


Now trying to run from the command line gives a TestNG Exception when
using this command 'java org.testng.TestNG testng.xml'.

Here is the output:

[ClassHelper] Could not instantiate tests.FirefoxTestRunner:
tests.FirefoxTestRunner
[ClassHelper] Could not instantiate tests.FirefoxTestRunner:
tests.FirefoxTestRunner
org.testng.TestNGException:
Cannot find class in classpath: tests.FirefoxTestRunner


My classpath from 'set CLASSPATH' shows all of these:

C:\projects\SeleniumTest\src
C:\projects\SeleniumTest\lib
C:\projects\SeleniumTest
C:\projects\SeleniumTest\src\utils
C:\projects\SeleniumTest\src\tests

Furthermore, my Build Path for my project shows it has the 'src'
directory under the Source tab. Also, Ctrl+Shift+T will pull up all 3
of my browser test runners.


But when I try to invoke the xml through the command line, it cannot
find the first test runner class. I'm assuming I made a stupid error
with my classpath... but I really have no idea. Any suggestions would
be greatly appreciated.


Thanks
Zack

Cédric Beust ♔

unread,
May 24, 2011, 12:44:51 PM5/24/11
to testng...@googlegroups.com
Hi Zack,

You don't seem to be very familiar with how the class path works since none of the directories you give below seem to contain any class file. Locate your .class files and set your classpath accordingly.

I strongly suggest you spend some time reading up a Java tutorial that explains how the classpath works, it will save you a lot of time down the line.

-- 
Cédric





--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To post to this group, send email to testng...@googlegroups.com.
To unsubscribe from this group, send email to testng-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.


Zack Lemley

unread,
May 24, 2011, 1:24:09 PM5/24/11
to testng-users
Cédric,

Thanks for the reply. No, I'm not very familiar with configuring
classpaths. And it makes sense that the .class files are the ones that
should be referenced in the classpath (doh!). I added the bin
directory which contains my .class files to the class path. I'm now
getting a different error...

[[TestNGClassFinder]] Unable to read methods on class
tests.FirefoxTestRunner - unable to resolve class reference com/google/
common/base/Function
Exception in thread "main" java.lang.NoClassDefError: com/google/
common/base/Function


Now I'm really lost haha.



Zack

Zack Lemley

unread,
May 24, 2011, 1:56:11 PM5/24/11
to testng-users
I did some more research and found out when I was setting the
classpath in the command line it wasn't looking at my projects
'.classpath' at all. Can I set where it looks for dependencies in the
testng.xml?

I believe you can do this with ant...


Zack

Cédric Beust ♔

unread,
May 24, 2011, 2:15:01 PM5/24/11
to testng...@googlegroups.com
.classpath is used by Eclipse only.

Again, please go read a tutorial that explains how to configure the class path, it will take you ten minutes...

-- 
Cédric

Ravi Gunasekaran

unread,
Sep 24, 2013, 12:00:03 AM9/24/13
to testng...@googlegroups.com
Hi,

  Please this solution to run the testNG from the Command Line it is working.

java -classpath "Path of TestNG.jar"; org.testng.TestNG "your_TestNG.xml"


If you want to run your code in standalone machine without Eclipse IDE.Create Executable .Jar file using eclipse and use below option to run the testNG from command prompt.

java -classpath"Path of TestNG.jar";"Your_classe_executable_Jar_path.jar"; . org.testng.TestNG  "your_TestNG.xml"


Regards,
G.Ravi
Reply all
Reply to author
Forward
0 new messages