What is TestNG framework

What is TestNG Framework?

What is TestNG Framework?

In this post, we are going to answer the question – what is TestNG framework? Why is it important for every automation tester? We will discuss the TestNG basics and take a simple example to see its usage. TestNG gives the ability to produce test run results in a flexible and easy to understand format.

NG in TestNG means Next Generation. TestNG is similar to JUnit but it is much more powerful than JUnit but still it’s inspired by Junit.

This is a beginner level tutorial on TestNG.

Basics of TestNG

TestNG is a simple to use framework. The ease of use yet powerful features have made it the preferred choice for reporting in automation script. TestNG uses annotations to help generate reports.

TestNG framework has pre-defined annotations, which can be used by automation script writers to generate useful reports.

TestNG can generate reports as shown below. This becomes more important as Selenium framework has no feature of generating useful reports.

TestNG Report

TestNG annotations are written indications to the Selenium engine to generate reports as automation scripts are being executed. One such example is shown below:

@AfterTest

public void closewebapp()

{

driver.close();

}

@AfterTest is a TestNG annotation, which indicates to the execution engine that, the web driver has to be closed and removed from the memory. So, two things happen here. The webdriver instance is closed as well as this even is logged as part of the report and that is made available to you as a report.

TestNG Annotations

There are multiple TestNG annotations and below is the complete list of all TestNG annotations:

TestNG Annotations

These annotations provide powerful mechanism to aid your automation scripts in generating useful reports.

TestNG Example

Let’s look at a simple example to understand how does TestNG work. The following automation script verifies the Title of GMAIL website. This automation script can be written as follows with the TestNG annotation:

package TestNGEx;

import org.openqa.selenium.*;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.testng.Assert;

import org.testng.annotations.AfterTest;

import org.testng.annotations.BeforeTest;

import org.testng.annotations.Test;

 

publicclass TestNG1 {

 

public WebDriver driver = new FirefoxDriver();

String baseUrl = “http://www.gmail.com”;

 

@BeforeTest

publicvoid openwebapp()

{

driver.get(baseUrl);

}

 

@Test

publicvoid assertitle()

{

driver.manage().window().maximize();

String expectedTitle = “Gmail”;

String actualTitle = driver.getTitle();

Assert.assertEquals(expectedTitle,actualTitle);

driver.close();

}

@AfterTest

publicvoidclosewebapp()

{

driver.close();

}

}

We have used three annotations in this example:

  • BeforeTest
  • Test
  • AfterTest

These annotations are indicators to the execution engine that scripts are to be run before the actual test script and after the test script. @Test annotation indicates the actual test script. In this case, it is the verification of Gmail title.

In order to make these TestNG annotations work, you need to create a testng.xml will as shown below:

<?xml version=”1.0″ encoding=”UTF-8″?>

<!DOCTYPE suite SYSTEM “http://testng.org/testng-1.0.dtd”>

<suite name=”Suite”>

<test name=”Test”>

<classes>

<class name=”TestNGEx.TestNG1” />

</classes>

</test> <!– Test –>

</suite> <!– Suite –>

You need to add packagename.classname in this testng.xml file.

TestNG Parameters

TestNG again gives us another interesting feature called TestNG Parameters. It allows you to pass parameters directly to your test methods using your testng.xml. For example, to login into a system using two parameters as username and password. These parameters are passed using TestNG Annotation.

We will discuss TestNG parameters in upcoming tutorials.

About Techcanvass

Techcanvass is a software training organization and offers certification courses in Business Analysis and automation testing. Our courses in selenium are:

Selenium Certification (CP-SAT) Training

Selenium Certification Course

 

 

 

 

 

Selenium Training with Java

Selenium Training Course

Cheers

Leave a Reply

Your email address will not be published. Required fields are marked *