Getting Started with TestNG

TestNG, where NG stands for “next generation” is a test automation framework inspired by JUnit (in Java) and NUnit (in C#). TestNG can be used for unit testing, functional testing, integration testing, and end-to-end testing.

TestNG has gained a lot of popularity quickly and is one of the most widely used testing frameworks among Java developers. It mainly uses Java annotations to configure and write test methods, much similar to JUnit.

1. Introduction of TestNG

Some features that TestNG has over JUnit:

  • Extra Before and After annotations such as Before/After Suite and Before/After Group
  • Dependency testing
  • More flexible grouping of test methods
  • Multi-threaded execution
  • In-built strong reporting framework

TestNG is written in Java and can be used with Java and other Java-related languages such as Groovy.

In TestNG, suites and tests are configured or described mainly through XML files. By default, the name of the file is testng.xml, but we can give it any other name if we want to.

TestNG allows the developers to write the test suites in XML files and enable them to include (or exclude) respective packages, classes, and methods in their test suite. It also allows developers to group test methods into particular named groups and to include or exclude them as part of the test execution.

2. Advantages of TestNG

Now let’s discover more features/advantages offered by TestNG.

  1. Multiple Before and After annotation options
  2. XML-based test configuration and test suite definition
  3. Dependent methods
  4. Named groups / Group of groups
  5. Dependent groups
  6. Parameterization of test methods
  7. Data-driven testing
  8. Multi-threaded test execution
  9. Better reporting

3. Installing TestNG into Eclipse

Before we can download and start using TestNG, we should have a minimum of Java 8 or above installed on our system.

Copy the latest update URL from the update site.

Use the above URL to install TestNG support in Eclipse. Go to the option: Help > Install New Software.

Install New Software Option

Click Add button and choose the required components to be installed.

Choose required components and click Next

Agree with any license agreement if asked. TestNG Support will be installed in the next few minutes in the Eclipse IDE.

It may ask to restart the IDE after finishing the installation. Please restart.

3. Creating Java Project with TestNG Dependencies

Before writing our first TestNG test, we have to create a Java project in Eclipse and include our TestNG dependencies.

Go to File | New | Other. A window with multiple options will be shown.

Select New Maven Project as shown in the following screenshot and click on Next.

Select the appropriate maven archetype.

Fill in the project’s Maven coordinates.

By default, Maven includes the Junit dependency in pom.xml file. We need to remove the JUnit dependency and include the latest TestNG dependency.


Now we are good to write our tests using TestNG.

4. Creating Our First TestNG class

Before this, you may delete any existing auto-creating JUnit test class, if any. Now, perform the following steps to create our first TestNG class:

1. Go to File | New | Other. This will open a new Add wizard window in Eclipse.

Add wizard window

2. Select TestNG class from the Add wizard window and click on Next.

Add wizard window-2

3. On the next window click on the Browse button and select the Java project where you need to add your class.

select the Java project

4. Enter the package name and the test class name and click on Finish.

Enter the package name

5. This window also gives you an option to select different annotations while creating a new TestNG class. If selected, the plugin will generate dummy methods for these annotations while generating the class. This will add a new TestNG class to our project.

package com.howtodoinjava.testng.examples;

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

public class AppTest 
  public void f() {
  public void beforeTest() {

  public void afterTest() {

We have successfully added a new TestNG test class to the newly created Java project in Eclipse. Feel free to modify the code as needed. Now let’s run the newly created test class through Eclipse.

5. Running TestNG test

5.1. Shortcut Options

Notice the test class in Eclipse IDE. You will see a few blurred texts as “Run All” or “Run | Debug“. These are the shortcuts we can use to run or debug a single test method or test class with a simple mouse click.

Click on these highlighted options to run tests

If you do not see the above shortcuts for any reason, then please follow the given steps to create the testNG run configuration and execute it.

5.2. Create Run Configuration

Perform the following steps to run tests through Eclipse:

1. Select the Java project in Eclipse and go to Run | Run Configuration.

Run Configuration for TestNG

2. Select TestNG in the given options and click on the New button to create a new configuration.

Create new TestNG configuration

3. Please notice that TestNG plugin provides multiple options for running your test cases as follows:

  • Class: Using this option you can provide the class name along with the package to run only the said specific test class.
  • Method: Using this you can run only a specific method in a test class.
  • Groups: In case you would like to run specific test methods belonging to a particular TestNG group, you can enter those here for executing them.
  • Package: If you would like to execute all the tests inside a package, you can specify these in this box.
  • Suite: In case you have suite files in the form of testing.xml files, you can select those here for execution.

Let’s enter the configuration name as TestNGRunConfig and select the newly created class under the Class section and click on Apply.

4. Now if you would like to run the newly created configuration, just click on Run after clicking on Apply.

This will compile and run the TestNG test class that we have written. The result of the test execution is displayed in the Console and Results in windows of Eclipse as shown in the following screenshot.

[TestNG] Running:

    Default test
    Tests run: 1, Failures: 0, Skips: 0

Default suite
Total tests run: 1, Failures: 0, Skips: 0

[TestNG] Time taken by org.testng.reporters.XMLReporter@177b3cd: 23 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 0 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@b8deef: 46 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@10ab323: 12 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@5e176f: 13 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@d1e89e: 142 ms

You can also run the test class by selecting it and then right-clicking on it, selecting Run as from the menu, and then choosing TestNG Test.

Run TestNG test class

In this TestNG tutorial, we learned about TestNG, features offered by TestNG, installing the TestNG plugin into Eclipse and writing and executing a TestNG tests through Eclipse.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

6 thoughts on “Getting Started with TestNG”

  1. Error occurred during initialization of boot layer
    java.lang.module.FindException: Unable to derive module descriptor for C:\Users\Qaisar.p2\pool\plugins\com.beust.jcommander_1.78.0.jar
    Caused by: java.lang.IllegalArgumentException: com.beust.jcommander.1.78.0: Invalid module name: ‘1’ is not a Java identifier

    These errors come when I run the test class.
    plz, guide me as soon as possible.

  2. my code is not working, @Test annnotation code is not working any more, there is no error shown in the class or anything but when i excute the code, beforeMethod annotation is working, while @test is not working, please help me out of this 🙁

  3. Can we test Spring based web application using this tool? Spring based web application used annotations and it has transactions and hibernate also.


Leave a Comment


A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.