TestNG - Plug With Eclipse

Advertisements


To set up TestNG with eclipse, following steps need to be followed:

Step 1: Download TestNG archive

Download http://www.testng.org

OSArchive name
Windowstestng-6.8.jar
Linuxtestng-6.8.jar
Mactestng-6.8.jar

Assume you copied above JAR file in C:\>TestNG folder.

Step 2: Set Eclipse environment

  • Open eclipse -> right click on project and click on property > Build Path > Configure Build Path and add the testng-6.8.jar in the libraries using Add External Jar button.

Add testng-6.8.jar in liraries.
  • We assume that your eclipse has inbuilt TestNG plug-in; if it is not available, then please get the latest version using the update site:

    • In your eclipse IDE, select Help / Software updates / Find and Install.

    • Search for new features to install.

    • New remote site.

    • For Eclipse 3.4 and above, enter http://beust.com/eclipse.

    • For Eclipse 3.3 and below, enter http://beust.com/eclipse1.

    • Make sure the check box next to URL is checked and click Next.

    • Eclipse will then guide you through the process.

Now, your eclipse is ready for the development of TestNG test cases.

Step 3: Verify TestNG installation in Eclipse

  • Create a project TestNGProject in eclipse at any location.

  • Create a class MessageUtil to test in the project.

   
/*
* This class prints the given message on console.
*/
public class MessageUtil {

   private String message;

   //Constructor
   //@param message to be printed
   public MessageUtil(String message){
      this.message = message;
   }
      
   // prints the message
   public String printMessage(){
      System.out.println(message);
      return message;
   }   
} 
  • Create a test class TestNGExample in the project.

   
import org.testng.Assert;
import org.testng.annotations.Test;

public class TestNGExample {
    String message = "Hello World";	
    MessageUtil messageUtil = new MessageUtil(message);

    @Test
    public void testPrintMessage() {	  
       Assert.assertEquals(message,messageUtil.printMessage());
   }
}

Following should be the project structure:

Project Structure

Finally, verify the output of the program by right clicking on program and running as TestNG.

Verify the result.

TestNG result success.

Advertisements
Advertisements