logo logo

TestNG Tutorial: Introduction, Installation, Features and Examples

TestNG Tutorial

Welcome to TestNG tutorial where I will go into detail all about this powerful test automation framework from start to finish: installation instructions, a list of features and advantages including a comparison between TestNG vs. JUnit, and will demonstrate test writing in the end.

Let’s begin!

Introduction to TestNG Tutorial Framework

TestNG is a test automation framework for Java, inspired by JUnit and NUnit and developed to overcome their limitations (NG – Next Generation). Its purpose is to cover all categories of test automation – unit testing, functional test, end-to-end, integration testing. Eliminating most of the limitations of the older framework – Junit (see down below in Features and Advantages), this Java test automation framework gives the developer the ability to write more flexible and powerful tests. Although, in order to be able to use this automation framework, you need to have at least a basic knowledge of Java programming language.


TestNG has various IDE plugins, for Eclipse, IntelliJ and NetBeans. We will cover the installation for IntelliJ – TestNG IDEA plug-in has a similar behavior as the built-in JUnit support and also provides the same output test view as JUnit tests.

The first step is to open IntelliJ and go to Settings> Plugins to check if the TestNG plugin is available. A list of available plugins will appear with TestNG-J:

TestNG tutorial: installation

  • If not available, click ‘Install JetBrains plugin’ and then install TestNG-J plugin. The TestNG installation for other development tools is similar to IntelliJ.
  • If you run IntelliJ the first time with the framework, you may have to create an initial configuration: Run> Edit Configurations and select it on the list:

TestNG tutorial: installation


The options for the set of tests that will be running:

  1. Package: Specify a package to run, all tests in this package and below will be included.
  2. Group: Specify a group to run.
  3. Suite: Specify an external testng.xml file to run.
  4. Class: Run all tests in a single class.
  5. Method: Run a single test method.

Once you have the configuration, you can run it.


Features and Advantages (including: TestNG vs. JUnit)

TestNG vs. Junit functionality

  • TestNG can do a lot more complex automation testing, because of its ability of grouping test cases and executing these groups. For example, if you have defined many test cases and assigned them to 2 different groups, you can execute any particular group passing its name to TestNG. Unlike TestNG, Junit cannot perform Group tests, have objects as parameters, or have dependencies between methods.
  • No need to extend any classes.
  • No constraint for mandatory annotations (in JUnit @BeforeClass and @AfterClass are mandatory).
  • There is no constraint for naming TestNG methods. You can give the method an arbitrary name.
  • In TestNG we can have dependency between methods, while in JUnit it is not possible.
  • TestNG allows us to create parallel tests.
  • TestNG has a wider range of SetUp/Teardown annotations, including @Before/AfterSuite, @Before/AfterTest and @Before/AfterGroup.
  • A list of attributes that we can pass to annotations. In this example we used priority attribute, that tells us what priority has the test method for execution. Methods with lower priority value will be executed first. Other important attributes that can be passed are dataProvider (used for parametrization), description (description of the method), invocationCount (number of times a method should be invoked), etc.
  • Asserts, that help us to verify the conditions of the test and decide whether it failed or passed. TestNG supports assertion of a test using the Assert class and assertion plays an important role when performing automation testing on an application. With asserts we can check if two values are equal (assertEquals, assertNotSame), check if the output values exist (assertNotNull), check conditions (assertTrue, assertFalse), etc.

Writing Tests

As an example in this TestNG tutorial, we will have two classes:

  1. SampleClass which contains methods.
  2. TestSampleClass which tests methods of the first class.

In SampleClass we will define two simple methods for string concatenation:

public class SampleClass
public String mergeStrings(String str1, String str2)
return str1.concat(str2);
public String mergeStringsWithDash(String str1, String str2)
return str1.concat("-").concat(str2);

TestSampleClass is intended to test defined methods in first class:

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

public class TestSampleClass
SampleClass sc;
String str1, str2;
public void initStringsAndObjects()
sc = new SampleClass();
str1 = "abc";
str2 = "def";
@Test (priority = 1)
public void testMergeStrings()
Assert.assertNotNull(sc.mergeStrings(str1, str2));

Assert.assertEquals(sc.mergeStrings(str1, str2), "abcdef");


@Test(priority = 2)
public void testMergeStringsWithDash()
Assert.assertNotNull(sc.mergeStringsWithDash(str1, str2));

Assert.assertEquals(sc.mergeStringsWithDash(str1, str2), "abc-def");

Assert.assertNotSame(sc.mergeStringsWithDash(str1, str2), "abcdef");

public void cleanup()
str1 = null;
str2 = null;
sc = null;

In the given example we introduced TestNG annotations and asserts which are TestNG basic methods used for automation testing. Same as JUnit, TestNG uses annotations to give the special meaning to methods:

@Test: that test method is being defined.
@BeforeTest: usually goes with methods for initialization of all required values (e.g. creating db connection, initializing required objects).
@AfterTest: usually goes with methods for cleanup after tests are performed (e.g. closing db connection).

Test Execution

TestNG plugin for IntelliJ provides an easy way to execute an automated test. Right click on test class and press Run to start the execution.

After the execution had started, output window will appear on the bottom. Assuming everything is working as planned, all tests will be green, and we will receive the output message “Process finished with exit code 0”:

All tests were successful:

TestNG tutorial: All tests were successful

Output message for a successful execution:

TestNG tutorial: Output message for a successful execution

We can simply make the test fail, by changing the expected value of a single assert in second method:

public void testMergeStringsWithDash()
Assert.assertNotNull(sc.mergeStringsWithDash(str1, str2));

Assert.assertEquals(sc.mergeStringsWithDash(str1, str2), "abc-defH");

Assert.assertNotSame(sc.mergeStringsWithDash(str1, str2), "abcdef");

In this case, the output will be a little bit different, test method testMergeStringsWithDash failed:

TestNG tutorial: testMergeStringsWithDash failed

When we select the failed method, output window will show more details about the failure, indicating the exact lines of code where the test failed. These are the test failure details:

TestNG: Test failure details

It is also possible to do the export from the console to HTML or XML. After the export to HTML, we get the following report, execution report:

TestNG tutorial: execution report
It is important to emphasize that report creation can be provided automatically with TestNG for individual tests or test groups.


-Do share this TestNG tutorial, I had fun making it 😉 . Is TestNG your favorite test automation framework? Which ups and downs have you experienced?


About the author

Miljan Radojičić

System Administration & Testing at Comtrade Group

Leave a Reply