Introduction to TestNG
TestNG is a powerful testing framework for Java that is widely used with Selenium WebDriver to organize, execute, manage, and report automated test cases. The name TestNG stands for Testing Next Generation. It provides features such as annotations, assertions, test suites, grouping, parameterization, data-driven testing, parallel execution, dependencies, and reporting.
In Selenium automation, TestNG acts as the test execution and test management layer. Selenium WebDriver performs browser automation, while TestNG controls when tests should run, how tests should be grouped, how test data should be supplied, and how execution results should be handled.
JustAcademy's Selenium Training includes TestNG as part of its automation testing curriculum, along with test annotations, assertions and validations, test execution flow, data-driven testing, and parallel test execution.
Course Resource: Selenium Training | Register for Course Demo
1. What is TestNG?
TestNG is an open-source Java testing framework inspired by JUnit and NUnit. It was designed to provide powerful and flexible testing capabilities for modern Java applications and automation frameworks.
TestNG is particularly useful in Selenium projects because it provides a structured way to create and execute automated browser test cases.
For example, instead of writing one large Selenium program containing login, search, cart, checkout, and logout operations, TestNG allows these scenarios to be organized as separate test methods and executed according to defined rules.
TestNG
|
+-- Test Classes
|
+-- Test Methods
|
+-- Annotations
|
+-- Assertions
|
+-- Test Suites
|
+-- Groups
|
+-- Parameters
|
+-- Data Providers
|
+-- Reports
|
+-- Parallel Execution
2. Why is TestNG Used with Selenium?
Selenium WebDriver is primarily responsible for controlling browsers and interacting with web applications. It does not by itself provide a complete test management framework.
TestNG fills this gap by providing test organization, execution control, assertions, test dependencies, parameterization, data-driven testing, grouping, parallel execution, and reporting.
| Selenium WebDriver | TestNG |
| Controls the browser | Controls test execution |
| Finds web elements | Organizes test methods |
| Clicks buttons | Defines execution order |
| Enters text | Provides assertions |
| Handles browser navigation | Provides test annotations |
| Automates web application actions | Supports data-driven testing |
| Supports multiple browsers | Supports parallel test execution |
| Does not manage complete test suites | Creates and manages test suites |
3. TestNG Architecture in Selenium Automation
A typical Selenium and TestNG automation architecture contains several layers. Selenium performs browser operations, TestNG manages test execution, and additional tools such as Maven, Git, reporting libraries, and CI/CD tools can support the overall automation framework.
Selenium Automation Framework
|
+-----+-----+
| |
TestNG Selenium WebDriver
| |
+----------+-------+ |
| | | |
Annotations Assertions Data |
| | | |
+----------+-------+ |
| |
Test Execution |
| |
+-------> Browser
|
Chrome / Firefox
Edge / Other Browsers
|
Web Application
4. Main Features of TestNG
- Annotations
- Assertions
- Test suites
- Test groups
- Test dependencies
- Parameterization
- Data-driven testing
- Parallel execution
- Test execution control
- Test prioritization
- Listeners
- Test reports
- Expected exceptions
- Timeout handling
- Configuration methods
5. Installing TestNG
TestNG can be added to a Java Selenium project using Maven. In a Maven project, the dependency is normally added to the pom.xml file.
Maven Dependency
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.10.2</version>
<scope>test</scope>
</dependency>
The exact TestNG version can be updated according to the project requirements and the version supported by the project's Java and build-tool setup.
6. First TestNG Program
The simplest TestNG test contains a Java class, a test method, and the @Test annotation.
import org.testng.annotations.Test;
public class FirstTest {
@Test
public void verifyTitle() {
System.out.println("TestNG test executed successfully");
}
}
The @Test annotation tells TestNG that the method should be treated as a test method.
7. TestNG @Test Annotation
The @Test annotation is the most commonly used TestNG annotation. It marks a method as a test case.
import org.testng.annotations.Test;
public class LoginTest {
@Test
public void loginTest() {
System.out.println("Login test executed");
}
}
When TestNG executes this class, the method marked with @Test is recognized as a test case.
8. Important TestNG Annotations
TestNG provides configuration annotations that allow setup and cleanup operations to be executed at different stages of the test lifecycle.
| Annotation | Purpose |
| @BeforeSuite | Runs before all tests in a suite |
| @AfterSuite | Runs after all tests in a suite |
| @BeforeTest | Runs before the test section defined in testng.xml |
| @AfterTest | Runs after the test section defined in testng.xml |
| @BeforeClass | Runs before the first test method in a class |
| @AfterClass | Runs after all test methods in a class |
| @BeforeMethod | Runs before each @Test method |
| @AfterMethod | Runs after each @Test method |
| @BeforeGroups | Runs before tests belonging to specified groups |
| @AfterGroups | Runs after tests belonging to specified groups |
| @Test | Defines a test method |
9. TestNG Execution Flow
Understanding the execution lifecycle is important when building Selenium automation frameworks.
@BeforeSuite
|
@BeforeTest
|
@BeforeClass
|
@BeforeMethod
|
@Test
|
@AfterMethod
|
@AfterClass
|
@AfterTest
|
@AfterSuite
The exact lifecycle becomes more important when a project contains multiple classes, suites, groups, and configuration methods.
10. Example of TestNG Annotations
import org.testng.annotations.*;
public class AnnotationExample {
@BeforeSuite
public void beforeSuite() {
System.out.println("Before Suite");
}
@BeforeClass
public void beforeClass() {
System.out.println("Before Class");
}
@BeforeMethod
public void beforeMethod() {
System.out.println("Before Method");
}
@Test
public void testLogin() {
System.out.println("Login Test");
}
@Test
public void testSearch() {
System.out.println("Search Test");
}
@AfterMethod
public void afterMethod() {
System.out.println("After Method");
}
@AfterClass
public void afterClass() {
System.out.println("After Class");
}
@AfterSuite
public void afterSuite() {
System.out.println("After Suite");
}
}
11. Assertions in TestNG
Assertions are used to verify whether the actual result of a test matches the expected result. They are essential in Selenium automation because performing a browser action is not enough; the automation script must also validate the application behavior.
For example, after opening a login page, the test may verify the page title, URL, success message, or presence of a dashboard.
Common Assertion Types
- assertEquals()
- assertNotEquals()
- assertTrue()
- assertFalse()
- assertNull()
- assertNotNull()
assertEquals Example
import org.testng.Assert;
import org.testng.annotations.Test;
public class AssertionTest {
@Test
public void verifyTitle() {
String actualTitle = "Amazon";
String expectedTitle = "Amazon";
Assert.assertEquals(actualTitle, expectedTitle);
}
}
12. assertTrue()
assertTrue() verifies that a condition is true.
Assert.assertTrue(driver.getTitle().contains("Amazon"));
If the condition is true, the assertion passes. If the condition is false, the test fails.
13. assertFalse()
assertFalse() verifies that a condition is false.
Assert.assertFalse(driver.getTitle().contains("InvalidTitle"));
14. Hard Assertions
TestNG's normal assertions are generally referred to as hard assertions. When a hard assertion fails, execution of the current test method stops at that assertion.
Assert.assertEquals(actual, expected);
System.out.println("This statement will not execute if assertion fails");
15. Soft Assertions
Soft assertions allow multiple validations to be performed before the test reports the accumulated failures.
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;
public class SoftAssertionTest {
@Test
public void verifyMultipleValues() {
SoftAssert softAssert = new SoftAssert();
softAssert.assertEquals("Google", "Google");
softAssert.assertTrue(10 > 5);
softAssert.assertEquals("Selenium", "Java");
softAssert.assertAll();
}
}
The assertAll() call is important because it reports the collected assertion failures.
16. Selenium WebDriver with TestNG
One of the most common uses of TestNG is executing Selenium WebDriver tests.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
public class GoogleTest {
@Test
public void verifyGoogleTitle() {
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com");
String title = driver.getTitle();
Assert.assertTrue(title.contains("Google"));
driver.quit();
}
}
In real-world frameworks, WebDriver creation and cleanup are usually moved into reusable setup classes instead of creating the driver directly inside every test method.
17. Using @BeforeMethod and @AfterMethod with Selenium
A better approach is to initialize the browser before each test and close it after each test.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.*;
public class BaseTest {
protected WebDriver driver;
@BeforeMethod
public void setUp() {
driver = new ChromeDriver();
driver.manage().window().maximize();
}
@AfterMethod
public void tearDown() {
if (driver != null) {
driver.quit();
}
}
}
A test class can then extend the base class.
import org.testng.Assert;
import org.testng.annotations.Test;
public class HomePageTest extends BaseTest {
@Test
public void verifyHomePage() {
driver.get("https://www.google.com");
Assert.assertTrue(driver.getTitle().contains("Google"));
}
}
18. Test Priority
TestNG allows a priority value to be assigned to test methods.
@Test(priority = 1)
public void loginTest() {
System.out.println("Login");
}
@Test(priority = 2)
public void searchTest() {
System.out.println("Search");
}
@Test(priority = 3)
public void logoutTest() {
System.out.println("Logout");
}
Lower priority values are generally executed before higher priority values. However, test cases should ideally remain independent rather than relying heavily on execution order.
19. Test Dependencies
TestNG allows one test method to depend on another test method.
@Test
public void login() {
System.out.println("Login successful");
}
@Test(dependsOnMethods = "login")
public void checkout() {
System.out.println("Checkout executed");
}
Here, the checkout test depends on the login test.
Although dependencies are useful in some scenarios, independent test design is generally easier to maintain and parallelize.
20. Test Groups
Groups allow related test cases to be categorized and executed together.
@Test(groups = "smoke")
public void loginTest() {
System.out.println("Login Smoke Test");
}
@Test(groups = "regression")
public void checkoutTest() {
System.out.println("Checkout Regression Test");
}
@Test(groups = {"smoke", "regression"})
public void searchTest() {
System.out.println("Search Test");
}
Groups are useful for creating smoke, sanity, regression, functional, integration, and other logical test categories.
21. Why Test Groups are Useful
| Group | Example |
| Smoke | Login, homepage, critical navigation |
| Sanity | Recently changed functionality |
| Regression | Complete existing functionality |
| Functional | Business functionality validation |
| Integration | Interaction between application components |
22. Parameterization in TestNG
Parameterization allows values to be supplied to test methods from the TestNG configuration file.
Java Example
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class ParameterTest {
@Test
@Parameters("browser")
public void launchBrowser(String browser) {
System.out.println("Browser: " + browser);
}
}
testng.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Automation Suite">
<test name="Browser Test">
<parameter name="browser" value="chrome"/>
<classes>
<class name="ParameterTest"/>
</classes>
</test>
</suite>
23. Data-Driven Testing
Data-driven testing means executing the same test logic with multiple sets of input data. TestNG provides the @DataProvider annotation for this purpose.
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class LoginDataTest {
@DataProvider(name = "loginData")
public Object[][] loginData() {
return new Object[][] {
{"user1", "password1"},
{"user2", "password2"},
{"user3", "password3"}
};
}
@Test(dataProvider = "loginData")
public void loginTest(String username, String password) {
System.out.println("Username: " + username);
System.out.println("Password: " + password);
}
}
The same test method is executed for each data set returned by the DataProvider.
24. DataProvider Flow
DataProvider
|
+-- Username 1 + Password 1
|
+-- Username 2 + Password 2
|
+-- Username 3 + Password 3
|
v
Same Test Method
|
v
Multiple Test Executions
25. Parallel Execution
TestNG can execute tests in parallel. Parallel execution can reduce overall execution time when a framework is designed correctly.
For example, Chrome, Firefox, and Edge tests can potentially run simultaneously using separate WebDriver instances.
<suite name="Parallel Suite" parallel="tests" thread-count="3">
<test name="Chrome Tests">
<parameter name="browser" value="chrome"/>
<classes>
<class name="LoginTest"/>
</classes>
</test>
<test name="Firefox Tests">
<parameter name="browser" value="firefox"/>
<classes>
<class name="LoginTest"/>
</classes>
</test>
<test name="Edge Tests">
<parameter name="browser" value="edge"/>
<classes>
<class name="LoginTest"/>
</classes>
</test>
</suite>
Parallel execution requires thread-safe framework design. Sharing one WebDriver instance between parallel tests can cause test interference.
26. TestNG testng.xml
The testng.xml file is commonly used to define test suites and control which tests, classes, methods, groups, and parameters should be executed.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Selenium Test Suite">
<test name="Login Tests">
<classes>
<class name="LoginTest"/>
</classes>
</test>
</suite>
27. Test Suite
A test suite is a collection of test cases or test classes that are executed together.
Test Suite
|
+-- Login Tests
|
+-- Search Tests
|
+-- Cart Tests
|
+-- Checkout Tests
|
+-- Logout Tests
For a large Selenium project, separate suites can be created for smoke testing, regression testing, sanity testing, browser testing, or release validation.
28. TestNG Listeners
Listeners allow automation frameworks to respond to different events during test execution.
For example, a listener can be used to capture screenshots when a test fails, log test execution information, or integrate execution results with reporting systems.
Common Listener Interfaces
- ITestListener
- ISuiteListener
- IInvokedMethodListener
- IRetryAnalyzer
Basic ITestListener Example
import org.testng.ITestListener;
import org.testng.ITestResult;
public class TestListener implements ITestListener {
@Override
public void onTestFailure(ITestResult result) {
System.out.println("Test Failed: " + result.getName());
}
@Override
public void onTestSuccess(ITestResult result) {
System.out.println("Test Passed: " + result.getName());
}
}
29. TestNG Retry Mechanism
Sometimes a test may fail because of temporary environmental or synchronization issues. TestNG supports retry mechanisms through IRetryAnalyzer.
import org.testng.IRetryAnalyzer;
import org.testng.ITestResult;
public class RetryAnalyzer implements IRetryAnalyzer {
private int count = 0;
private final int maxRetry = 2;
@Override
public boolean retry(ITestResult result) {
if (count < maxRetry) {
count++;
return true;
}
return false;
}
}
Retry should be used carefully. It should not be used to hide genuine application defects or poorly synchronized automation.
30. Expected Exceptions
TestNG allows developers to specify exceptions that are expected during a test.
@Test(expectedExceptions = ArithmeticException.class)
public void divisionTest() {
int result = 10 / 0;
}
The test passes when the expected exception occurs.
31. Test Timeout
TestNG allows a timeout to be specified for a test method.
@Test(timeOut = 5000)
public void testPageLoad() {
System.out.println("Test must complete within the configured timeout");
}
Timeouts should be chosen carefully. Selenium synchronization should generally use appropriate waits rather than relying on arbitrary long timeouts.
32. TestNG with Page Object Model
TestNG is commonly combined with the Page Object Model (POM). POM separates page-specific locators and actions from test logic.
src/test/java
|
+-- base
| +-- BaseTest.java
|
+-- pages
| +-- LoginPage.java
| +-- HomePage.java
|
+-- tests
| +-- LoginTest.java
|
+-- utilities
| +-- WaitUtils.java
| +-- ScreenshotUtils.java
|
+-- testng.xml
LoginPage.java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class LoginPage {
private WebDriver driver;
private By username = By.id("username");
private By password = By.id("password");
private By loginButton = By.id("login");
public LoginPage(WebDriver driver) {
this.driver = driver;
}
public void enterUsername(String value) {
driver.findElement(username).sendKeys(value);
}
public void enterPassword(String value) {
driver.findElement(password).sendKeys(value);
}
public void clickLogin() {
driver.findElement(loginButton).click();
}
public void login(String user, String pass) {
enterUsername(user);
enterPassword(pass);
clickLogin();
}
}
LoginTest.java
import org.testng.Assert;
import org.testng.annotations.Test;
public class LoginTest extends BaseTest {
@Test
public void validLoginTest() {
driver.get("https://example.com/login");
LoginPage loginPage = new LoginPage(driver);
loginPage.login("admin", "admin123");
Assert.assertTrue(
driver.getCurrentUrl().contains("dashboard")
);
}
}
33. TestNG with Maven
Maven manages project dependencies and provides a standard mechanism for building and executing Java automation projects.
Project
|
+-- pom.xml
|
+-- src
| |
| +-- main
| |
| +-- test
|
+-- testng.xml
The Maven Surefire Plugin is commonly used to execute TestNG tests during Maven test execution.
Example Surefire Configuration
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.5.2</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
34. Running TestNG Tests
TestNG tests can commonly be executed through:
- IDE such as IntelliJ IDEA
- Eclipse
- TestNG suite XML
- Maven
- Jenkins or another CI/CD platform
- Command-line build processes
35. Selenium + TestNG Execution Flow
Developer writes test
|
v
TestNG @Test method
|
v
@BeforeMethod
|
v
WebDriver initialization
|
v
Open application
|
v
Perform Selenium actions
|
v
Assertions / Validations
|
v
Test Result
/ \
PASS FAIL
| |
v v
Report Screenshot/Log
|
v
@AfterMethod
|
v
Driver Quit
36. TestNG Reports
TestNG generates execution information that can help identify passed, failed, and skipped tests. In larger automation frameworks, TestNG results can also be integrated with reporting tools such as ExtentReports or Allure.
| Result | Meaning |
| PASS | Test completed successfully |
| FAIL | Test encountered a failure |
| SKIP | Test was not executed, often due to dependency or configuration conditions |
37. TestNG in a Real Automation Framework
In a real project, TestNG is generally one component of a larger framework.
Selenium Automation Framework
|
+--------------------+--------------------+
| | |
TestNG Selenium WebDriver Maven
| | |
+-----+------+ | Dependency Mgmt
| | |
Annotations Assertions Browser
| | |
DataProvider Validation Web Application
|
Parallel Execution
|
Reports / Listeners
|
CI/CD
|
Jenkins / Git
38. Advantages of TestNG
- Easy integration with Selenium WebDriver.
- Provides powerful annotations.
- Supports assertions and validations.
- Supports data-driven testing.
- Supports parameterization.
- Supports test grouping.
- Supports test dependencies.
- Supports parallel test execution.
- Supports listeners.
- Supports retry mechanisms.
- Supports test suites.
- Provides execution reports.
- Works well with Maven and CI/CD tools.
- Helps organize large automation projects.
39. TestNG vs JUnit
| Feature | TestNG | JUnit |
| Java testing framework | Yes | Yes |
| Annotations | Extensive configuration annotations | Extensive annotation support |
| DataProvider | Built-in @DataProvider | Uses parameterized testing mechanisms |
| Groups | Built-in grouping support | Uses tags/categories depending on version |
| Parallel execution | Supported | Supported through modern JUnit mechanisms and configuration |
| Suite XML | testng.xml | Different suite/configuration mechanisms |
| Dependency support | dependsOnMethods / dependsOnGroups | Different approach |
| Selenium usage | Very common | Also commonly used |
40. TestNG vs Selenium
| Selenium | TestNG |
| Browser automation toolset | Java testing framework |
| Automates browser actions | Manages test execution |
| Finds and interacts with elements | Validates results using assertions |
| Controls browser sessions | Controls test lifecycle |
| Does not define complete test suites | Defines test suites |
| Provides WebDriver APIs | Provides annotations and execution features |
41. Common TestNG Project Structure
SeleniumTestNGProject
|
+-- pom.xml
|
+-- testng.xml
|
+-- src
| |
| +-- test
| |
| +-- java
| |
| +-- base
| | +-- BaseTest.java
| |
| +-- pages
| | +-- LoginPage.java
| | +-- HomePage.java
| |
| +-- tests
| | +-- LoginTest.java
| | +-- SearchTest.java
| |
| +-- utilities
| +-- WaitUtils.java
| +-- ScreenshotUtils.java
|
+-- reports
|
+-- screenshots
42. Practical Example: Login Automation
Consider an e-commerce website containing a login page. The automation requirement is to validate successful login.
Test Steps
- Open the browser.
- Navigate to the login page.
- Enter username.
- Enter password.
- Click Login.
- Wait for the dashboard.
- Validate the dashboard.
- Capture screenshot if the test fails.
- Close the browser.
Execution Flow
TestNG
|
v
@BeforeMethod
|
v
Start Chrome
|
v
Open Login Page
|
v
Enter Credentials
|
v
Click Login
|
v
Dashboard
|
v
Assert Dashboard
|
+---- PASS
|
+---- FAIL -> Screenshot
|
v
@AfterMethod
|
v
Quit Browser
43. Common Mistakes in TestNG
Mistake 1: Forgetting @Test
public void loginTest() {
// TestNG may not recognize this as a test method
}
Correct:
@Test
public void loginTest() {
// Test method
}
Mistake 2: Forgetting assertAll() with SoftAssert
SoftAssert softAssert = new SoftAssert();
softAssert.assertTrue(true);
softAssert.assertEquals("A", "B");
softAssert.assertAll();
Mistake 3: Sharing WebDriver in Parallel Tests
When tests execute in parallel, each test thread should generally have its own properly managed WebDriver instance.
Mistake 4: Using Test Dependencies Everywhere
Excessive dependencies can make test suites fragile. Independent tests are usually easier to execute and maintain.
Mistake 5: Using Priority as the Main Test Design Strategy
Priority should not be used as a substitute for proper test independence and framework architecture.
44. Best Practices for TestNG
- Keep test methods focused on test behavior.
- Use meaningful test method names.
- Use assertions for important validations.
- Keep setup and cleanup inside appropriate configuration methods.
- Use DataProvider for data-driven scenarios.
- Use groups for smoke, regression, and other execution categories.
- Keep tests independent wherever practical.
- Use Page Object Model for maintainability.
- Use reusable utility methods.
- Capture screenshots for useful failure evidence.
- Use listeners for framework-level behavior.
- Use parallel execution only with thread-safe design.
- Integrate TestNG execution with Maven and CI/CD.
- Avoid hard-coded credentials and sensitive information.
- Keep test data separate from test logic when appropriate.
45. TestNG with CI/CD
TestNG can be integrated into CI/CD workflows so that automated Selenium tests execute as part of a build or deployment pipeline.
Developer
|
v
Git Push
|
v
Jenkins / CI Server
|
v
Maven Build
|
v
TestNG Suite
|
v
Selenium Tests
|
v
Browser Execution
|
v
Test Results
|
+---- PASS
|
+---- FAIL
|
v
Reports / Notifications
TestNG can therefore form an important part of a larger Selenium automation workflow involving framework architecture, reporting, logging, version control, and CI/CD.
46. Real-World Use Cases of TestNG
- Login testing
- Registration testing
- E-commerce testing
- Shopping cart testing
- Checkout testing
- Search functionality testing
- Regression testing
- Smoke testing
- Cross-browser testing
- Data-driven testing
- End-to-end testing
- Parallel test execution
- CI/CD automation
47. Interview Questions on TestNG
Q1. What is TestNG?
TestNG is a Java testing framework used to organize, execute, validate, and manage automated test cases. It is commonly used with Selenium WebDriver.
Q2. What does TestNG stand for?
TestNG stands for Testing Next Generation.
Q3. What is the purpose of @Test?
The @Test annotation marks a method as a TestNG test method.
Q4. What is the difference between @BeforeMethod and @BeforeClass?
@BeforeMethod runs before each test method, while @BeforeClass runs once before the first test method of the class.
Q5. What is @AfterMethod?
@AfterMethod runs after each @Test method and is commonly used for cleanup operations.
Q6. What is DataProvider?
DataProvider supplies multiple sets of test data to a test method and is commonly used for data-driven testing.
Q7. What is testng.xml?
testng.xml is a TestNG suite configuration file used to define and control test execution.
Q8. What is the difference between hard and soft assertions?
A hard assertion stops the current test method when it fails, whereas a SoftAssert can collect multiple failures and report them when assertAll() is called.
Q9. What are TestNG groups?
Groups categorize related test methods so selected categories such as smoke or regression tests can be executed together.
Q10. What is parallel execution?
Parallel execution means running multiple tests or test groups concurrently to reduce total execution time.
Q11. What are TestNG listeners?
Listeners provide hooks into test execution events and can be used for logging, screenshots, reporting, retry mechanisms, and other framework-level functionality.
Q12. Can TestNG be integrated with Selenium?
Yes. TestNG is commonly used with Selenium WebDriver to organize, execute, validate, and report browser automation tests.
48. Quick Revision
| Concept | Purpose |
| TestNG | Java test execution and management framework |
| @Test | Defines a test method |
| @BeforeMethod | Runs before each test |
| @AfterMethod | Runs after each test |
| Assert | Validates expected vs actual results |
| DataProvider | Supports data-driven testing |
| Groups | Organizes tests into categories |
| Priority | Controls relative execution order |
| Dependencies | Defines relationships between tests |
| testng.xml | Defines suites and execution configuration |
| Listener | Handles test execution events |
| Parallel Execution | Runs tests concurrently |
| Maven | Build and dependency management |
| Selenium | Browser automation |
49. Learning Roadmap: Selenium + TestNG
Core Java
|
v
Selenium WebDriver
|
v
Locators & WebElements
|
v
Browser Interactions
|
v
Waits & Synchronization
|
v
TestNG
|
+-- Annotations
+-- Assertions
+-- Groups
+-- Parameters
+-- DataProvider
+-- Listeners
+-- Parallel Execution
|
v
Page Object Model
|
v
Automation Framework
|
v
Maven + Git
|
v
Reporting
|
v
Jenkins / CI/CD
|
v
Real-Time Automation Project
50. Summary
TestNG is an important component of Java-based Selenium automation. Selenium WebDriver is responsible for browser automation, while TestNG provides the structure needed to organize and execute automated tests.
Important TestNG concepts include @Test, @BeforeMethod, @AfterMethod, assertions, test suites, groups, priorities, dependencies, parameters, DataProvider, listeners, retry mechanisms, and parallel execution.
When combined with Selenium WebDriver, Page Object Model, Maven, Git, reporting tools, and CI/CD systems, TestNG can become an important part of a maintainable and scalable test automation framework.
51. Course Resources
Learn more about Selenium automation, WebDriver, TestNG, framework development, data-driven testing, POM, reporting, and related automation concepts through the following resources:
Selenium Training
Register for Course Demo