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.