What is TestNG?
TestNG is a testing framework used to organize, execute, and manage automated test cases.
It is heavily used with Selenium WebDriver.
TestNG provides:
-
Test grouping (Smoke, Regression, Module-wise)
-
Test execution ordering & prioritization
-
Parallel browser execution (runs tests faster)
-
Data-driven testing (DataProvider support)
-
Better reports
In short:
JUnit is okay for unit tests.
TestNG is built for real automation frameworks.
Why TestNG is Preferred Over JUnit (Real Industry Reason)
| Feature | JUnit | TestNG |
|---|---|---|
| Parallel Execution | Difficult | Very easy (parallel="classes") |
| Grouping Tests | Limited | Powerful & clear grouping support |
| Data Providers | Not built-in | Built-in DataProvider annotation |
| Reporting | Basic | Detailed HTML + XML reports |
| Executing multiple classes easily | Manual setup | testng.xml handles everything |
Interview tip:
Scalability is the REAL reason.
Automation frameworks in companies may contain hundreds of test cases — TestNG handles them cleanly.
Installing TestNG — Full Setup Guide
We’ll cover both IDEs, because students use both:
-
IntelliJ IDEA
-
Eclipse IDE
We will also handle both Maven and Non-Maven projects.
✅ If You Are Using IntelliJ IDEA (Recommended)
Step 1: Create a Maven Project
File → New → Project → Maven → Next → Finish
Step 2: Open pom.xml and Add TestNG Dependency
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.10.2</version>
<scope>test</scope>
</dependency>
Step 3: Let IntelliJ Download Libraries
Right Click on project → Maven → Reimport
or simply wait for IntelliJ to auto-resolve dependencies.
How to Verify TestNG was added:
Project → External Libraries → testng-7.x.jar should appear
✅ If You Are Using Eclipse
Step 1: Install TestNG Plugin
Help → Eclipse Marketplace → Search “TestNG” → Install → Restart Eclipse
Step 2: Create Java/Maven Project
File → New → Maven Project
or
File → New → Java Project (Non-Maven)
Step 3 (Maven Case): Add TestNG Dependency in pom.xml
Same as IntelliJ.
Step 3 (Non-Maven Case): Download TestNG Manually
-
Go to: https://testng.org
-
Download
.jar -
Add to Build Path:
Right Click Project → Build Path → Configure Build Path → Add External JARs
Verify Installation
Create class → Type
@Test→ Press Enter → It should auto-import from:import org.testng.annotations.Test;
If this import is suggested → TestNG is installed successfully.
✅ Your First TestNG Test (Same in IntelliJ & Eclipse)
import org.testng.annotations.Test;
public class FirstTest {
@Test // Marks this method as a test case
public void testExample() {
System.out.println("TestNG is working!");
}
}
Explanation Table
| Code Part | Meaning |
|---|---|
@Test |
Tells TestNG to treat this method as a test case |
testExample() |
Test method name (you can rename based on scenario) |
System.out.println(...) |
Simple output to confirm execution |
Run This Test
-
IntelliJ: Right click → Run ‘FirstTest’
-
Eclipse: Right click → Run As → TestNG Test
✅ Output Example
TestNG is working!
PASSED: testExample
===============================================
Total tests run: 1, Failures: 0, Skips: 0
===============================================
This confirms TestNG is successfully installed and working.
Understanding How TestNG Executes Tests
When you run the test:
-
TestNG searches for methods marked with
@Test -
Executes them in alphabetical order (unless priority is defined)
-
Generates a test result report
This is how TestNG controls execution.
Recommended Folder Structure (Real Selenium Framework)
src
└── test
├── java
│ ├── tests ← All test cases
│ ├── pages ← Page Object Model classes
│ └── utilities ← Helpers: WebDriver, config, waits, logs
└── resources
└── testng.xml ← Controls groups, suites, parallel runs
This is the exact structure used in real companies.
Why This Matters
This structure:
-
Keeps test cases organized
-
Separates UI page elements from test logic
-
Makes framework scalable for real projects
This is the structure interviewers expect you to know.
Where TestNG Fits in Automation Architecture
Selenium → Talks to Browser
TestNG → Controls Testing Flow
Java → Implements Logic
Jenkins/GitHub Actions → Runs tests daily
Allure/Extent → Reporting Layer
Think of:
-
Selenium = Driver
-
TestNG = Project Manager
Interview Questions from This Topic
| Question | Correct Good Answer Approach |
|---|---|
| What is TestNG? | Test automation framework used for organizing, grouping & executing tests efficiently. Supports parallel execution and data-driven testing. |
| Why use TestNG instead of JUnit? | More scalable, supports test grouping, prioritization, parallel runs, better reporting, and DataProviders. |
| What is testng.xml used for? | To configure and execute test suites, groups, and parallel executions without changing code. |
| Does TestNG replace Selenium? | No. Selenium handles browser automation; TestNG controls test execution. |