TestNG- The Best Unit Test Framework for Selenium with Java

TestNg stands for testing next generation, it is a unit test framework which is inspired from Junit and Nunit but has many new functionalities which makes it the most sought after framework. In this article we will learn how can we setup TestNG into our eclipse environment, and use it design and execute our test suites in Selenium. Also its intuitive reporting provides an added advantage to view and manage reports with ease.

Install TestNG in Eclipse

To install TestNG in the Eclipse environment, we have to perform the following steps –

  1. Select Help / Install New Software...
  2. Enter the update site URL in "Work with:" field:
  3. The URL for the product - http://beust.com/eclipse

Or use the Eclipse Market Place to install TestNG

TestNG Hierarchy and Annotations.

The TestNG follows a hierarchy in where the main node is Suite, which contains Tests. The Test contain Classes, the class will contain Methods. In terms of the artifacts, the TestNG XML file denotes a Suite. It is defined using the tag. This tag will contain the node. There could be one or more than one node. This will contain the node. The class is associated with the java file which contains atleast one TestNG annotation, and contains one or more test method, which is denoted by @Test in the class. 

The annotations in the TestNG follows the @Before… and @After… pattern. Any set of code written within for example beneath @Before suite will execute prior to suite execution starting. Similarly if we have an annotation used, @AfterClass it will execute the code lines, after every class execution within the tag. 

TestNG XML

The TestNG XML file is used to execute TestNG. A XML file is associated with the suite and follows the following sample template-

As seen in the above file, it is a suite which contains two tests. Test name nopackage contains one class, and the  other class contains two classes for execution.

Executing TestNG

We generally put the TestNG xml file for execution. And there are different ways we can execute the suite file. We can put the classes, in same test or different tests. We can put test methods from the same class or different class within groups for execution. We can also tests execution to run in parallel, depending on what we wish to run in parallel. Do we want to run the tests in parallel or the classes, or the methods inside the classes in parallel. This gives us a huge power of utilizing time efficiently and run suite judiciously.

We can execute only the failed testng tests, by picking information from the testng-failed.xml in the output directory. This file contains the name of the tests and other information required to run the tests which failed.

We can also run testng tests programmatically e.g.

.

Results and Logging

By default the TestNg suite execution generates an output directory called as test-output. This directory contains the test results file. Generally three types of result files are generated which presents the same information in different formats. These are – index.html, Emailable-report.html, and inside the suite named folder the test’s.html file name depending on the name of suite and tests given in the testng xml file. 

A sample result folder looks like this –

And the file contents like this –

 We can also generate our own result file by implementing listeners like ITestListener, and IReporter, more on which we will discuss in the upcoming series in TestNG.

For generating logs we can use Reporter.Log method, which allows us to capture the flow of execution and other log information in the result files.

Whats Next

In the next series of TestNG, we are going to discuss on the concepts of how we can use testng with selenium. How can we parameterize the testng tests, and generate our own reports. Let us know whether you liked reading this post, and what else would you like to know about TestNG. Your feedback always helps. Happy Testing!