Chapter 1: TestNG Framework
Up until now, you have been writing Selenium code inside a main() method. You launch a browser, do some actions, print "Test passed" or "Test failed" to the console, and move on. That works for learning, but it is not how real companies run thousands of tests every night.
Think of it like cooking. When you cook at home, you just start making whatever you want. No structure, no rules. But if you run a restaurant kitchen, you need stations, timings, checklists, and a system. TestNG is that system for your test automation.
TestNG stands for "Test Next Generation." It is a Java testing framework inspired by JUnit and NUnit. In the Selenium world, TestNG is the industry standard. Over 80% of Java-based Selenium frameworks use TestNG. When interviewers ask about your framework, they expect to hear "TestNG."
Without a framework, you are writing code like this:
public class NoFrameworkTest {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get("https://www.testerrank.com/banking");
// Login test
driver.findElement(By.id("username")).sendKeys("testuser");
driver.findElement(By.id("password")).sendKeys("pass123");
driver.findElement(By.cssSelector("button[type='submit']")).click();
// Check if login worked... but how?
if (driver.getCurrentUrl().contains("/dashboard")) {
System.out.println("Login test PASSED");
} else {
System.out.println("Login test FAILED");
}
// What if the test crashes here? Browser stays open forever.
// What about running 500 tests? All in one main()?
// What about reports? Who reads console output?
// What about running tests in parallel? Impossible with main().
driver.quit();
}
}This approach has serious problems. There is no automatic cleanup if a test crashes. You cannot run tests independently — one failure can kill the whole suite. There are no reports, no grouping, no parallel execution, and no way to retry flaky tests. TestNG solves all of these.
Q: What is TestNG and why do you use it in your framework?
A: TestNG is a Java testing framework — "Test Next Generation." I use it because it provides annotations for test lifecycle management (@BeforeMethod, @AfterMethod for browser setup/teardown), assertions for validation, DataProviders for data-driven testing, parallel execution to reduce suite time, testng.xml for suite configuration, and listeners for screenshots on failure and retry logic. It is the industry standard for Java-based Selenium automation.
You will hear about JUnit in interviews. Here is the honest comparison:
| TestNG | JUnit 5 |
|---|---|
| More annotation levels (Suite, Test, Groups, Class, Method) | Fewer lifecycle hooks (@BeforeAll, @BeforeEach) |
| Native DataProvider for data-driven tests | @ParameterizedTest with different syntax |
| Built-in testng.xml suite configuration | Uses Maven Surefire for configuration |
| Native groups with @Test(groups = {"smoke"}) | Uses @Tag for grouping |
| Built-in parallel at suite/test/class/method levels | Needs extra config for parallel execution |
| Auto-generates HTML reports | Needs plugins for HTML reports |
| Industry standard for Selenium QA teams | More popular with developers, less with QA |
In interviews, if asked "TestNG or JUnit?", say: "Both are good frameworks. I use TestNG because it has richer features for Selenium automation — more annotations, built-in DataProviders, native parallel execution, and testng.xml for suite management. Most QA teams in the industry use TestNG." This shows maturity — you are not bashing JUnit, just explaining your preference.
Key Point: TestNG is the industry standard for Java Selenium automation. It replaces main() with a structured, professional framework.