How to run TestNG test in cmd

2,278 views
Skip to first unread message

Grega G.

unread,
Aug 9, 2011, 6:36:06 AM8/9/11
to testng-users
Hi!

I run my tests under Eclipse with TestNG. I have two files. First I
have .java class and the second file is testng.xml.

I run my test through the Eclipse where I run my .xml file. But now I
would like to do so that I could run my test in cmd.

Any suggestion how to do that??

Thanks.

Krishnan Mahadevan

unread,
Aug 9, 2011, 11:47:45 AM8/9/11
to testng...@googlegroups.com
Grega,

My humble apologies for not responding to your personal note in time earlier (Was stuck with some other stuff and was also trying to figure out this myself)

Here's how you can do it:

Download the testng jar from : http://testng.org/doc/download.html
Create a batch file which will have the following:

java -cp testng-6.1.1.jar;projects\MyProject\target\test-classes org.testng.TestNG projects\MyProject\src\test\resources\MyCustomSuite.xml

Explanation :
  • If you notice that I am specifying the class path as "-cp"
  • I have not specified any path for testng-6.1.1.jar, since it is available in my current directory.
  • I am appending the classpath (Note that I used ";" to append paths) with projects\MyProject\target\test-classes [This is where java should find my ClassA.class and ClassB.class files]
  • org.testng.TestNG is the main class from which execution of your suite should begin.
  • projects\MyProject\src\test\resources\MyCustomSuite.xml is the relative path of my suite.xml file from my current directory.
  • MyCustomSuite.xml looks like below:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="false">
    <parameter name="environment.toRun" value="QA" />
    <test name="Test1" preserve-order="true">
        <classes>
            <class name="testng.samples.ClassB">
                <methods>
                    <include name="iRunFirst"></include>
                    <include name="iRunSecond"></include>
                </methods>
            </class>
            <class name="testng.samples.ClassA">
                <methods>
                    <include name="iRunFirst"></include>
                    <include name="iRunSecond"></include>
                </methods>
            </class>
        </classes>
    </test>
</suite>


Note:
If you would like to have the capabilities of kicking off runs from outside eclipse, you should seriously consider build tools such as either Ant or Maven.
[ I have been using only Maven, which is why it took me sometime to figure out how to make use of just java and get the execution done from the command line. But it was a good learning :) ]


For using Ant to get your executions done, please read more at : http://testng.org/doc/ant.html

For using Maven to kick off your testng executions, please read more at : http://testng.org/doc/maven.html

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"




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


Reply all
Reply to author
Forward
0 new messages