Wednesday 28 January 2015

TestNG

TestNG is an open-source automated testing framework inspired from JUnit and NUnit but introducing some new functionalities that make it more powerful and easier to use. Functionality such as use of annotations for dependency testing, test in groups, email-able reporting, multi-threaded testing, data-driven testing, flexible plug-in API etc..

TestNG is designed to cover all categories of tests such as unit, functional, end-to-end, integration, etc..

In this post we will see a simple example how to use TestNG to execute test cases. In this post we are using following tools:
  1. Eclipse IDE
  2. Maven
First, Add the TestNG plugin in Eclipse either using the eclipse market place or Install new software option, Now Create a Maven project in Eclipse and add following dependency in pom.xml and build the project:

<dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.8.7</version>
        <scope>test</scope>
</dependency>

Code:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class LaunchBrowser {

 WebDriver driver;

 @BeforeClass
 public void setup(){
  System.setProperty("webdriver.chrome.driver",
    "E:/WorkSpace/Drivers/chromedriver.exe");
  driver = new ChromeDriver();
 }


 @Test
 public void LaunchPage(){
  try{
   driver.get("http://www.google.com/");
  }catch(Exception e){
   e.printStackTrace();
  }
 }
 
 @AfterClass
 public void tearDown(){
  driver.quit();
 }

}

In this example we are using TestNG annotations to drive the test.

@BeforeClass: To execute the method before the first test method of the class is invoked.
@Test: Mark the method as part of the test.
@AfterClass: To execute the method after all the test method of the current class have been run.

Now to run the test do right-click on the workspace and select Run as >> TestNG Test option. After completion of the execution you can see the report generated at following location:

Project root\test-output\emailable-report.html











Stay tuned to see next post on more about TestNG.

Facebook Page: https://www.facebook.com/crazzzygig

Please like and comment on our post and page if you like the blog.

References: http://testng.org/