Tuesday, May 5, 2009


TestNG is yet an another framework to work in JAVA. Both looks almost same on the surface but there are vast differences between their frameworks. JUnit is designed to do the unit testing, and it's doing it very well while the TestNG has designed to do the higher level thing. So there are many features available in the TestNG that you will not find in JUnit.

Before we checkout the features provided by the TestNG, let's take a look at how to make the simple testcase using TestNG and run it.

TestNG Annotations
Following are some of the annotations we have used in our example
@BeforeClass: The annotated method will be run before the first test method in the current class is invoked.
@AfterClass: The annotated method will be run after all the test methods in the current class have been run.
@Parameters: Describes how to pass parameters to a @Test method.
@Test: Marks a class or a method as part of the test.

If you look at the @BeforeClass and @AfterClass annotation they are same as of JUnit, the only difference is the method annoted with @BeforeClass or @AfterClass need not to be static as compared to the JUnit.

TestNG Testcase


import static org.testng.AssertJUnit.*;
import org.testng.annotations.*;
import com.thoughtworks.selenium.*;

public class Google {

private Selenium selenium;

public void startSelenium(String host, String port, String browser, String url) {

this.selenium = new DefaultSelenium(host, Integer.parseInt(port), browser, url);

public void stopSelenium() {

public void googling(String search, String expected) {
try {
selenium.type("name=q", search);

} catch (SeleniumException e) {

Configuration file.
Configuration file is a normal xml file. which defines all the test cases, test suits, their names,all the parameters and their values and many more things.

Let's look at configuraion file for the above testcase

Before you run your testcase from the command line, You need to set the classpath.
Find out the file "testng-5.9-jdk15.jar" and set it's path in classpath variable.
The name of the file may differ depending upon the version of TestNG you are using.
To set the class path do as follows:
1. just right click on My Computer icon, Click on the Advance tab, then click on the Environment Button.
2. In the System Variable double click on the CLASSPATH.
3. In the variable value add the full path of your "testng-5.9-jdk15.jar" file.
Run the Testcase.
  • Make sure that the Selenium server is running.
  • Save the above Google.java file at your desired location
  • Compile the file using the following command on the shell prompt.
    javac Google.java
  • Run the file using the following command on the shell prompt.
    java org.testng.TestNG GoogleTestng.xml
  • Use the following command to see the result.
    start test-output\index.html

Once you will run the testcase on more interesting thing you will find about the TestNG, the reporting. TestNg has built in primary reporting facility which will let you know how many test cases from the particular test suit has run, how many of then passed and how many of them failed.


Anonymous said...

I am having problem compiling Google.java
error message says ""package org.testng doesn't exist'
when i'm running your code google.java in eclipse and run as testng it's fine but when i'm trying use the above example and complie it i get error.
please help

Unknown said...

Hi sushma,
This is because you haven't set the classpath variable.
Just check the Set the Classpath section.

Unknown said...


Could you please provide the web driver source code for the above program ?


vini said...


Click command not working in webdriver 2.30 in selenium in IE9. any one know how to resolve this?

Post a Comment