Sunday, February 7, 2010

Running TestNG tests from command line

Note: Eclipse is not required to compile or run TestNG tests.
This is how you can compile and run testng tests from command line. Follow the steps below
1. Download and Install JDK from http://java.sun.com/javase/downloads/index.jsp
2. The below bat file compiles and runs the dataProviderExample test explained in my earlier blog post. Read that post before trying this.
3. The project folder is at C:\MyEclipseWorkspace\dataDrivenTestProject. You would have to modify javaTestProjectPath variable in the bat file if your project folder lies elsewhere.
4. Java development kit or JDK is installed at C:\Program Files\java\jdk1.6.0_18, You need to change this if your jdk folder path is different from this. This will happen if you are using a different version of java than mine.
5. The testng.xml file given below is placed at C:\MyEclipseWorkspace\dataDrivenTestProject.

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="dataDrivenTestProject">
<test name="script.dataProviderExample">
<classes>
<class name="script.dataProviderExample"/>
</classes>
</test>
</suite>

6. Open Notepad and copy paste the below commands. Unselect word wrap from format-->word wrap in Notepad. This will ensure that there are no unwanted line breaks. Now save the file as testng.bat anywhere in your local machine. Double click the bat file to run.

REM create variable that stores the project folder path. This variable will used in the subsequent statements.
set javaTestProjectPath=C:\MyEclipseWorkspace\dataDrivenTestProject

REM move to the project folder
c:
cd
%javaTestProjectPath%
REM set path to dir that contains javac.exe and java.exe

set path=C:\Program Files\java\jdk1.6.0_18\bin
REM set the classpath, this tells java where to look for the library files, the project bin folder is adde as it will store the .class file after compile
set classpath=%javaTestProjectPath%\bin;%javaTestProjectPath%\Lib\junit-4.5.jar;%javaTestProjectPath%\Lib\jxl.jar;%javaTestProjectPath%\Lib\selenium-java-client-driver.jar;%javaTestProjectPath%\Lib\selenium-server.jar;%javaTestProjectPath%\Lib\testng-5.9-jdk15.jar
REM compile the dataProviderExample.java file, the -d parameter tells javac where to put the .class file that is created on compile
javac -verbose %javaTestProjectPath%\test\script\dataProviderExample.java -d %javaTestProjectPath%\bin
REM execute testng framework by giving the path of the testng.xml file as a parameter. The xml file tells testng what test to run
java org.testng.TestNG %javaTestProjectPath%\testng.xml


Read the REM statements in the bat file to understand the commands used.



11 Comments:

Prabakar Bangalore said...

Could you demonstrate an application testing using Selenium with "Maven2" ?

QualityPoint said...

First of all. Thank you very much for your useful post.

I just came across your blog and wanted to drop you a note telling you how impressed I was with the information you have posted here.

Please let me introduce you some info related to this post and I hope that it is useful for software testing community.

There is a good eBook for Software Testing & QTP, Have a look @

http://qualitypointtech.net/ebook/

Vamsi said...

Very useful posting. Please let me know if you would be interested in a consulting opportunity. We are a services/consulting company. I have a project to kick start automation for a e-commerce based company in Los Angeles, CA. I would like to hire your services for the same. My email is vputrevu@numentica.com. Please let me know if you are interested.

software testing company said...

Very useful steps for software testing. You have explained everything in simple term which is quite easy to understand.

SEO Perth said...

Very Good posting! you really amazed me with your posting tips. thanks and hope to see more ideas with you..

SEO Perth said...

Really good points.
TestNG has been giving me a headache and this has certainly helped.

SEO said...

will be interesting to see the result from this technique

keshav kumar said...

getting "Exception in thread "main" java.lang.NoClassDefFoundError: org/testng/TestNG" error when executing please help

Ramachand said...

Thanks. this is very nice share. Let me also recommend some good tutorials on QTP where you can find similar tips:

Read all the related Posts:


Basic of VBScript Language for QTP

Introduction to QTP (QuickTest Professional) Part2

Introduction to QTP (QuickTest Professional) Part3

Introduction to QTP (QuickTest Professional) Part4

Basic of VBScript Language for QTP

Shruti Ramalingam said...

Thanks for the great information in your blog Selenium Training in Chennai

Automation testing services said...

Great post. Impressive thanks for sharing