What is TestNG: A Beginner’s Guide to Selenium Automation


TestNG is a testing framework widely used with Selenium for automation projects. It extends the capabilities of JUnit and makes it easier to organize, manage, and run test cases at scale. With TestNG, testers can group tests, set priorities, run them in parallel, and generate detailed reports. This level of control is important when automation suites grow in size and complexity.
This guide explains what TestNG is, outlines its benefits, highlights differences with JUnit, and walks through the setup, configuration, and execution of test suites using TestNG
What is TestNG
TestNG, short for Test Next Generation, is a Java-based testing framework developed to overcome the limitations of JUnit and NUnit. It is not limited to unit testing but supports a wide range of testing needs, including functional, integration, end-to-end, and regression testing. This makes it suitable for both small projects and enterprise-scale automation.
Unlike basic test runners, TestNG provides a structured way to design and manage tests. Test cases can be grouped, prioritized, parameterized, and executed in parallel. This allows testers to run complex scenarios efficiently while keeping the code organized.
Key points that define TestNG include:
- Multi-level testing: Supports unit, functional, and integration testing within the same framework.
Annotation-driven - model: Uses annotations like @Test, @BeforeSuite, and @AfterClass to control execution flow.
- Advanced configuration: XML-based test suite configuration enables grouping, dependencies, and parallel execution.
- Reporting support: Generates detailed HTML and XML reports to analyze test results.
Benefits of Using TestNG in Automation
The true value of TestNG becomes clear when managing test cases in larger or evolving automation projects. It is not just about running individual tests but about organizing and controlling them in a way that supports reliability and scale.
Here are the main benefits that TestNG brings to automation:
- Flexible Test Configuration: TestNG allows grouping of test cases, defining dependencies between them, and setting execution priorities. This flexibility makes it easier to control test flow without hardcoding conditions in the test scripts.
- Parallel Test Execution: Running tests in parallel reduces execution time significantly, especially for projects with hundreds or thousands of test cases. Parallelism can be controlled at the method, class, or suite level through XML configuration.
- Annotation Support: With annotations such as @BeforeSuite, @AfterMethod, and @Test, the execution sequence of test cases becomes predictable and easy to manage. This reduces the overhead of writing boilerplate code to handle setup and teardown.
- Parameterization: TestNG makes it straightforward to pass parameters into test methods, either through XML files or data providers. This supports data-driven testing without repetitive code.
- Detailed Reporting: It automatically generates HTML and XML reports that summarize passed, failed, and skipped tests. These reports provide visibility into the test execution cycle and help in debugging failures.
- Integration with Build Tools: TestNG integrates well with Maven, Gradle, Jenkins, and other CI/CD tools, ensuring that tests can be executed automatically as part of continuous integration pipelines.
Key Differences Between TestNG and JUnit for Selenium Testing
JUnit and TestNG are both Java-based frameworks, but they serve different purposes. JUnit was designed with a focus on unit testing, while TestNG was created to handle broader testing needs, including functional and integration tests. For Selenium automation, TestNG is often preferred because of its wider feature set and better support for complex scenarios.
Below is a side-by-side comparison of their key differences:
Feature | JUnit | TestNG |
Scope | Primarily unit testing | Unit, functional, integration, regression, end-to-end testing |
Annotations | Limited (@Before, @After, @Test) | Extensive (@BeforeSuite, @AfterSuite, @BeforeClass, @AfterMethod, etc.) |
Parameterized Testing | Basic support | Multiple options (XML parameters, Data Providers) |
Test Grouping | Not supported | Supports grouping and dependencies |
Parallel Execution | Limited, external setup needed | Built-in support at method, class, or suite level |
Reporting | Minimal, needs plugins | Detailed HTML and XML reports generated automatically |
Integration | Works with build tools but less flexible | Strong integration with Maven, Gradle, Jenkins, and CI/CD tools |
TestNG Annotations and Their Use Cases
Annotations are one of the core features that make TestNG powerful. They define how and when different parts of the test code are executed. Instead of writing custom logic to set up and tear down test environments, annotations let testers specify the execution flow in a clear and consistent way. This makes test scripts easier to read, maintain, and scale.
Below are the most commonly used TestNG annotations and their purpose:
- @BeforeSuite/@AfterSuite: Run once before or after all tests in a suite. Useful for setting up global configurations such as database connections or closing them after execution.
- @BeforeTest/@AfterTest: Run before or after <test> tags defined in the XML file. Often used for initializing browser instances and cleaning up after tests.
- @BeforeClass/@AfterClass: Run once before or after all test methods in a class. Commonly used for class-level setup such as preparing page objects.
- @BeforeMethod/@AfterMethod: Run before or after each test method. Typically used for resetting states or logging test results.
- @Test: Marks a method as a test case. It supports attributes like priority, groups, enabled, and dependsOnMethods, which give testers fine-grained control over execution.
- @DataProvider: Supplies data to a test method, enabling parameterized or data-driven testing.
- @Parameters: Passes values directly from the XML file to test methods, useful for environment-specific variables like browser type.
Understanding Test Suites in TestNG
In TestNG, a test suite is a collection of test cases that are executed together. Instead of running individual test classes one by one, a suite allows testers to organize and manage related tests in a single execution cycle.
This is especially useful when working on large Selenium projects where different types of tests such as smoke, regression, or functional tests need to be grouped and run based on project requirements.
Here are the main elements of a TestNG suite:
- Suite Level: The highest level in the XML configuration. It can contain multiple <test> tags, each representing a different set of test cases.
- Test Level: Defines a collection of test classes or methods. For example, one <test> tag could include all smoke tests, while another handles regression tests.
- Class Level: Specifies which Java classes should be executed. This helps group related test methods under the same class.
- Method Level: Allows running specific test methods within a class, offering precise control over execution.
How to Create and Configure a Test Suite
Creating a test suite in TestNG involves writing an XML configuration file that defines how tests should be executed. This file provides a central way to organize test classes, set execution rules, and control parameters without modifying the Java test code.
The process begins by creating a file named testng.xml in the project directory. Inside this file, the <suite> tag acts as the root element. Within the suite, testers can define multiple <test> blocks, and each block can contain one or more <classes>. At the class level, individual test classes are declared, and even specific test methods can be included or excluded.
Here are the typical configuration options that can be set in a TestNG suite:
- Suite Name: Declared in the <suite> tag. Helps identify the suite when generating reports.
- Parallel Execution: Enabled by setting attributes like parallel=”methods” or parallel=”tests”. This reduces execution time for larger test suites.
- Thread Count: Specifies how many threads will run in parallel when parallel execution is enabled.
- Test Groups: Allows running only selected groups of tests, such as smoke or regression, using the groups attribute.
- Included and Excluded Methods: Provides control to run only specific methods from a class or exclude certain tests as needed.
- Parameters: Supports passing values directly into test methods, such as browser type or environment settings, which makes tests more flexible.
Writing a TestNG XML File to Execute Test Suites
The testng.xml file is the backbone of TestNG configuration. It defines which tests to run, how they should run, and under what conditions. This file provides a flexible way to organize execution without touching the Java code.
By editing the XML, testers can quickly switch between different sets of tests, enable or disable parallel execution, and pass parameters to test methods.
A basic example of a TestNG XML file looks like this:
Key elements in the file:
- <suite>: The root element. The name attribute identifies the suite, and attributes like parallel and thread-count manage parallel execution.
- <test>: Groups together related test classes. Each <test> can have its own configuration.
- <classes> and <class>: Define which Java classes contain the test methods to be executed.
- <methods> (optional): Allows inclusion or exclusion of specific test methods inside a class.
Prerequisites for Setting Up TestNG
Before TestNG can be used with Selenium, certain prerequisites need to be in place. These ensure that the environment is ready for test creation and execution. Setting them up correctly helps avoid configuration issues later in the project.
The following requirements should be met before starting with TestNG:
1. Java Development Kit (JDK)
TestNG is a Java-based framework, so a compatible JDK must be installed and configured with the JAVA_HOME environment variable. Most teams use JDK 8 or higher.
2. Integrated Development Environment (IDE)
An IDE such as Eclipse or IntelliJ IDEA is recommended. Both provide plugins for TestNG, making it easier to write, run, and debug tests.
3. Maven or Gradle
A build automation tool helps manage dependencies. Adding TestNG as a dependency in a pom.xml (Maven) or build.gradle (Gradle) file ensures consistent setup across all environments.
4. Selenium WebDriver
Since TestNG is often used with Selenium, WebDriver libraries for the target browsers must be included in the project.
5. TestNG Library
Either install it through the IDE’s plugin marketplace or add it as a dependency in the build tool. For Maven, the dependency looks like this:
org.testng
testng
7.9.0
test
Once these prerequisites are ready, the framework can be configured to write and execute TestNG-based Selenium tests without additional setup overhead.
How to Run TestNG in Selenium: Step-by-Step Example
Running TestNG with Selenium involves creating test classes, configuring them with annotations, and then executing them through an XML suite file or directly from the IDE. The steps below outline a simple but complete example.
Step 1: Create a Test Class
Write a Java class that contains Selenium test methods annotated with @Test.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class LoginTest {
WebDriver driver;
@BeforeClass
public void setUp() {
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://example.com/login");
}
@Test
public void validLoginTest() {
// Selenium code to perform login and verify success
}
@Test
public void invalidLoginTest() {
// Selenium code to perform login with wrong credentials and verify error message
}
@AfterClass
public void tearDown() {
driver.quit();
}
}
Step 2: Create the TestNG XML File
Define how this class should run by creating a testng.xml file.
Step 3: Run the Suite
In Eclipse or IntelliJ, right-click the testng.xml file and select Run As > TestNG Suite.
If using Maven, execute the command:
mvn clean test
Step 4: Review the Reports
After execution, TestNG generates reports in the test-output folder. Reports include an overview of passed, failed, and skipped tests, along with execution time details.
This workflow demonstrates how Selenium test scripts integrate with TestNG to provide structured execution and detailed results.
Best Practices for Writing TestNG Tests
Writing TestNG tests is not only about using annotations correctly but also about maintaining a framework that is reliable, scalable, and easy to work with. Following best practices ensures that tests provide meaningful results and remain manageable as the project grows.
Here are the key practices to follow when working with TestNG:
- Use meaningful test names: Method names should describe the purpose of the test clearly, for example verifyValidLogin() instead of test1().
- Group tests logically: Assign tests to groups such as smoke, regression, or functional. This allows selective execution based on project requirements.
- Leverage annotations effectively: Use @BeforeMethod and @AfterMethod for test setup and cleanup to avoid code duplication.
- Parameterize wherever possible: Use @Parameters or @DataProvider for input variations to reduce repetitive test code.
- Keep test classes focused: A test class should target one area of functionality rather than mixing unrelated tests.
- Avoid hard-coded values: Externalize test data and configurations using property files, XML, or a database.
- Use dependencies cautiously: While dependsOnMethods is useful, avoid creating long dependency chains that make debugging harder.
- Enable parallel execution wisely: Run tests in parallel for speed, but ensure tests are thread-safe to avoid false failures.
Conclusion
TestNG provides the structure and flexibility needed for managing test automation in Selenium projects. Its annotation model, suite configuration through XML, and built-in reporting make it suitable for projects ranging from small functional checks to enterprise-level regression suites.
Running these tests locally is often not enough when projects need to validate across different browsers, versions, and operating systems. With BrowserStack, teams can execute TestNG tests on 3,500+ real browsers and devices in the cloud. This eliminates the need for setting up and maintaining local infrastructure while ensuring that applications are tested under conditions identical to end users.

Contents
- What is TestNG
- Benefits of Using TestNG in Automation
- Key Differences Between TestNG and JUnit for Selenium Testing
- TestNG Annotations and Their Use Cases
- Understanding Test Suites in TestNG
- How to Create and Configure a Test Suite
- Writing a TestNG XML File to Execute Test Suites
- Prerequisites for Setting Up TestNG
- How to Run TestNG in Selenium: Step-by-Step Example
- Best Practices for Writing TestNG Tests
- Conclusion
Subscribe for latest updates
Share this article
Related posts




