What is TestNG and How to use TestNG in Selenium | TestNG Selenium Tutorial

TestNG in Selenium

What is TestNG

TestNG in Selenium
TestNG Features

TestNG is a Testing framework which helps in effective execution of automated test cases. TestNG in selenium could be understood as a layer which not only facilitates in test execution management (for eg. parallel test execution, setting the test execution priority, creating separate test suites as per requirement etc.) but also in Reporting, logging, data provider (parameterization) etc. as described in the above figure. In this TestNG selenium tutorial, we will look upon all the TestNG features mentioned in the figure.

Installing TestNG

Eclipse does not ship with TestNG, hence we will first install TestNG in eclipse. Navigate to Help -> Install New Software

 
TestNG in Selenium
 

This will open a new Install window (see below). Locate the address bar next to Work With: and enter the following url:

Click on Add button and select the TestNG checkbox.

 TestNG in Selenium

Once you have made your selections as in the above image, click on Next and start with the TestNG installation.

If you are prompted to enter name after you click on Add button, provide any name, preferrably TestNG.

To verify the installation is successful, right click on any of the existing package and select New -> Other. This will open Select a Wizard pop up window. Enter TestNG in the Wizards search box and it should display the TestNG Class.

Before we start writing our test scripts using TestNG it is important to remember below points regarding TestNG:

  • TestNG is basically a Class.
  • In TestNG classes, we only write our test scripts and other annotations (more on it after the next few lines).
  • All other logic and helper classes should be written outside the TestNG class.
  • We can create a testng.xml to facilitate execution of multiple classes in one go. This feature is discussed in detail under Annotations section.

Above TestNG attributes should become more clear once we start writing our tests.

Creating our first TestNG class

Select File -> New -> Java Project and create a new java project SeleniumTestNG. Proceed with steps 1, 2 and 3 as described in the following figure.

TestNG in Selenium

Annotations in TestNG

You must have already observed @Test keyword in our TestNG class. This keyword is known as an annotation and is always followed by a method or function. This method should be the code for a test case. A TestNG class may have multiple @Test annotations, and thus multiple methods (multiple test scripts).

Suppose we have to automate 3 manual test cases: Login, Search and Logout. To make things simple, we will only print some text under these tests and going forward will scale this class to give you an actual picture of how TestNG is used in real projects.

Update the default code with the following code:

package testpkg;

import org.testng.annotations.Test;

public class NewTest {
  @Test
  public void loginTest(){
      System.out.println("Under Login Test");
  }
  
  @Test
  public void searchTest(){
      System.out.println("Under Search Test");
  }
  
  @Test
  public void logoutTest(){
      System.out.println("Under Logout Test");
  }
}

If you get any error for testng package, mouse hover the testng import line (second line) and select Add TestNG Library option.

To run the TestNG class, just right click and select Run As -> TestNG Test

TestNG in Selenium
Since we have 3 @Test in our TestNG class, results on console are displayed as PASSED three times for each @Test (method). Also, the execution of the test cases is not in the order they are written. logoutTest() is written in the last but it gets executed before searchTest(). By default, TestNG tests run in alphabetical order.

There is one extra tab when we execute TestNG class, Results of running class <className>. This tab presents the execution summary in a more user-friendly way. Click on this tab and explore yourself. Since we have kept our test scripts simple and they all pass, there is not much in TestNG Results tab.

Reporting in TestNG

TestNG generates a default report after test execution is completed. TestNG report is quite readable and we get this functionality out of the box from TestNG.

To generate TestNG report for the above project once the execution is completed, select the project, right-click and select Refresh. A new folder test-output will be created under the project.

TestNG in Selenium

If you open the index.html file in Chrome, it will display a complete execution report for the TestNG class we have created. Explore the report yourself and see what all it offers.

TestNG in Selenium

More Annotations

@BeforeMethod and @AfterMethod

TestNG offers some more annotations which makes tester’s life easy. Consider an example where you have to close the browser and open it after each @Test. TestNG extends its help in rescuing us from this regressive task.

The annotation @BeforeMethod will be executed before each @Test. Similarly, @AfterMethod will be executed after the execution of @Test. Following code will make things more clear:

package testpkg;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class NewTest {
    @BeforeMethod
    public void openBrowser() {
        System.out.println("Open Browser");
    }
    
    
  @Test
  public void loginTest(){
      System.out.println("Under Login Test");
  }
  
  @Test
  public void searchTest(){
      System.out.println("Under Search Test");
  }
  
  @Test
  public void logoutTest(){
      System.out.println("Under Logout Test");
  }
  
    @AfterMethod
    public void closeBrowser() {
        System.out.println("Close Browser");
    }
}

New annotations are highlighted in the above code. We already know that each @Test represents one test case and each test case one method is written. Code written in @BeforeMethod annotation will always run before each @Test is executed. Similarly, @AfterMethod will be executed after each @Test. For the above example, @BeforeMethod and @AfterMethod code will be executed 3 times each. The output of above program is:

TestNG in Selenium

@BeforeTest and @AfterTest

Since TestNG contains the @Test annotation or methods which represent a test TestNG class is also known as Test class. Suppose we need to fetch some values from an excel sheet before starting our tests, we will utilize @BeforeTest and @AfterTest annotations.

@BeforeTest method will be executed before any test and even before @BeforeMethod annotation. Similarly, @AfterTest method will be executed after all @Test and @AfterMethod are executed. 

Unlike @BeforeMethod and @AfterMethod these annotations will be executed only once. Changes to the code are in bold:

package testpkg;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class NewTest {
    @BeforeTest
    public void beforeTestAnnotation() {
        System.out.println("Fetching data from excel");
    }
    
    @BeforeMethod
    public void openBrowser() {
        System.out.println("Open Browser");
    }
    
    
  @Test
  public void loginTest(){
      System.out.println("Under Login Test");
  }
  
  @Test
  public void searchTest(){
      System.out.println("Under Search Test");
  }
  
  @Test
  public void logoutTest(){
      System.out.println("Under Logout Test");
  }
  
    @AfterMethod
    public void closeBrowser() {
        System.out.println("Close Browser");
    }
    
    @AfterTest
    public void afterTestAnnotation() {
        System.out.println("Closing the excel file");
    }
}

Console output after executing the above script:

TestNG in Selenium

It is evident from the above output that @BeforeTest and @AfterTest are executed only once.

@BeforeSuite and @AfterSuite

A suite is a collection of all the test scripts in our project. So far, we have been working on a single test class containing 3 tests, but in real projects there would be more than one test class, each containing multiple test scripts. We use testng.xml to execute all the test classes in one go.

@BeforeSuite and @AfterSuite annotations are executed only once for the whole suite (all test classes). Update your code with these annotations as below:

package testpkg;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class NewTest {
    @BeforeSuite
    public void beforeSuiteAnnotation() {
        System.out.println("Under Before Suite");
    }
    
    @BeforeTest
    public void beforeTestAnnotation() {
        System.out.println("Fetching data from excel");
    }
    
    @BeforeMethod
    public void openBrowser() {
        System.out.println("Open Browser");
    }
    
    
  @Test
  public void loginTest(){
      System.out.println("Under Login Test");
  }
  
  @Test
  public void searchTest(){
      System.out.println("Under Search Test");
  }
  
  @Test
  public void logoutTest(){
      System.out.println("Under Logout Test");
  }
  
    @AfterMethod
    public void closeBrowser() {
        System.out.println("Close Browser");
    }
    
    @AfterTest
    public void afterTestAnnotation() {
        System.out.println("Closing the excel file");
    }
    
    @AfterSuite
    public void afterSuiteAnnotation() {
        System.out.println("Under After Suite");
    }
}

Next, we will create one more test class with 1 @Test and try to execute the whole suite (project) using testng.xml. Create a new java class SecondTestClass and write the following test in it:

package testpkg;

import org.testng.annotations.Test;

public class SecondTestClass {

    @Test
    public void secondTestClass() {
        System.out.println("In Second Test class");
    }
    
}

Both of the classes are under the same package testpkg.

testng.xml

The testng.xml file is the main configuration file which manages the execution order, test scripts to be executed/ excluded and a single source of test execution. Once we create this file, we are no longer required to run each class individually. We can specify the test classes in this XML file and all these classes will start executing one by one. If we specify the order of execution for the @Test or methods, then the @Test will be executed sequentially.

Below is the standard template for testng.xml. To create an XML file, select the project folder, right click and select New -> File and in the pop-up window write testng.xml and hit Finish button. This should create a blank testng.xml file at the root of the project. Open this file and copy paste below code in the Source tab of testng.xml.

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="SeleniumTestNG suite">

    <test name="First Test">
        <classes>
            <class name="testpkg.NewTest"></class>
        </classes>
    </test>

    <test name="Second Test">
        <classes>
            <class name="testpkg.SecondTestClass"></class>
        </classes>
    </test>
</suite>

suite name and test name could be any value. class name should contain the packagename.classname. To execute all the test scripts (@Test) of all the test classes in our project, right click on the testng.xml and select Run As -> TestNG Suite. Below is the console output after execution:

TestNG in Selenium

Total tests run are 4 (3 test in NewTest and 1 test in SecondTestClass). Also, notice that @BeforeSuite is executed first and only once before any other annotation. Similarly, @AfterSuite is executed only once after all the annotations from all the classes are executed.

Don’t get confused between @BeforeTest/ @BeforeSuite and @AfterTest/ @AfterSuite. @BeforeTest and @AfterTest are applied at class level whereas @BeforeSuite and @AfterSuite are always executed before and after the whole test and any other annotations.

Also, take a look at the index.html report and explore it.

Parameters (Data Providers) in TestNG

The concept of a data provider in TestNG is to execute the same test case with multiple sets of data. Suppose we have to write a test script to validate login for 2 different users, then either we have to write the login method twice for each user or we can simply use data provider feature provided by TestNG.

Create a new java class DataProviderTestNG inside the same testpkg package:

package testpkg;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class DataProviderTestNG {
    
    //Write the dataProvider name
    @Test(dataProvider="getData")
    public void multipleUserLoginTest(String userName, String password, String securityQuestion) {
        System.out.println("User " + userName + " has password " + password + " and security question is " + securityQuestion);
    }

    @DataProvider
    public Object[][] getData(){
        //This data is for 2 users having three values- userName, password and securityQuestion
        Object[][] data = new Object[2][3];
        
        //Data for first user
        data[0][0] = "name_one";
        data[0][1] = "pass_one";
        data[0][2] = "name_one_question";
        
        //data for second user
        data[1][0] = "name_two";
        data[1][1] = "pass_two";
        data[1][2] = "name_two_question";
        
        return data;
    }
}

The new annotation @DataProvider denotes the test data. In the above example, we have created a 2D object array which contains information of 2 users- userName, password, and securityQuestion. getData() method simply puts the information of each user in the array object and return the object.

We have modified our @Test annotation to include dataProvider=”getData” which tells the test to repeat itself until all the values under @DataProvider are executed. Next, we will update our existing testng.xml (highlighted in bold) to execute this test class also.

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="SeleniumTestNG suite">

    <test name="First Test">
        <classes>
            <class name="testpkg.NewTest"></class>
        </classes>
    </test>

    <test name="Second Test">
        <classes>
            <class name="testpkg.SecondTestClass"></class>
        </classes>
    </test>
    
    <test name="Third Test">
        <classes>
            <class name="testpkg.DataProviderTestNG"></class>
        </classes>
    </test>
    
</suite>

If you look at the output of our last executed testng.xml, total tests run count is 4. Execute the testng.xml again and the total tests run count will be changed to 6 since our data provider test class will execute twice.

TestNG in Selenium

Include and Exclude test methods from execution in TestNG

Suppose you have 10 test cases in your suite, spread across multiple test classes and you want to execute only a few out of these 10. This can be achieved using <methods>, <include> and <exclude> tags in testng.xml.

For instance, we want to execute only loginTest() from NewTest.java class, exclude the test method secondTestClass() from SecondTestClass.java and all execute all the methods under DataProviderTestNG.java class. Modify the testng.xml as following:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="SeleniumTestNG suite">

    <test name="First Test">
        <classes>
            <class name="testpkg.NewTest">
                <methods>
            <include name="loginTest" />
            </methods>
            </class>
        </classes>
    </test>

    <test name="Second Test">
        <classes>
            <class name="testpkg.SecondTestClass">
            <methods>
            <exclude name="secondTestClass" />
            </methods>
            </class>
        </classes>
    </test>
    
    <test name="Third Test">
        <classes>
            <class name="testpkg.DataProviderTestNG"></class>
        </classes>
    </test>
    
</suite>

We started with <methods> tag in NewTest and used <include> tag to execute the loginTest test method defined under it. Similarly, for SecondTestClass we used <exclude> tag to exclude the secondTestClass test method from execution. If you run this modified testng.xml, it will result in 3 as the total test run count. You may use multiple <include> and <exclude> tags inside a single test class to restrict or control the test execution.

Author: Dhawal Joshi

A post-graduate in MCA, ISTQB & ITIL certified QA with more than 8 years of experience in QA working with a CMMI Level 5 organization as System Analyst. I started my automation journey with HP UFT(formerly known as QTP) and for the past few years, I am using Selenium for automation. I also have experience in Android Application Development, Java, HTML, and VBScript. When I am not working, I like to spend time with my family, cooking and learning new developments in IT.

5 thoughts on “What is TestNG and How to use TestNG in Selenium | TestNG Selenium Tutorial

Leave a Reply

Your e-mail address will not be published. Required fields are marked *