testNG -Unit Testing Framework
π 1. What is TestNG?
TestNG (Test Next Generation) is a testing framework inspired by JUnit and NUnit.
It was created by CΓ©dric Beust to overcome the limitations of JUnit (3 & 4) and make testing more powerful.
It supports unit testing, functional testing, integration testing, and end-to-end testing.
π 2. Why TestNG came?
Before TestNG, most people used JUnit in Java. But:
β JUnit did not support parallel execution.
β JUnit had limited annotations.
β Grouping tests (e.g., smoke vs regression) was not possible.
β No built-in reporting β only console output.
β Parameterization was hard to achieve.
π To solve these problems, TestNG was introduced.
π 3. What problems does TestNG solve?
β
Supports annotations (easy to define pre/post conditions).
β
Allows grouping (smoke, regression, sanity).
β
Supports parallel execution β saves time.
β
Provides dependency testing (dependsOnMethods, dependsOnGroups).
β
Built-in HTML reports.
β
Allows parameterization (via testng.xml or @DataProvider).
β
Integrates seamlessly with Maven, Jenkins, Selenium.
π 4. Structure of TestNG Framework
Letβs break the TestNG structure into 3 levels:
πΉ (A) TestNG Annotations
These control the lifecycle of tests.
| Annotation | Meaning | Example |
@BeforeSuite | Runs before entire suite | DB connection |
@AfterSuite | Runs after entire suite | Close DB connection |
@BeforeTest | Runs before <test> block in XML | Setup browser |
@AfterTest | Runs after <test> block in XML | Quit browser |
@BeforeClass | Runs before class methods | Login |
@AfterClass | Runs after class methods | Logout |
@BeforeMethod | Runs before each test method | Navigate to page |
@AfterMethod | Runs after each test method | Clear cookies |
@Test | Actual test case | Verify login |
π Example:
public class LoginTest {
@BeforeClass
public void setup() {
System.out.println("Launch browser");
}
@Test(priority = 1)
public void validLogin() {
System.out.println("Valid login test");
}
@Test(priority = 2)
public void invalidLogin() {
System.out.println("Invalid login test");
}
@AfterClass
public void tearDown() {
System.out.println("Close browser");
}
}
πΉ (B) testng.xml (Suite File)
The test suite configuration file.
Example:
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="RegressionSuite" parallel="false">
<parameter name="browser" value="chrome"/>
<parameter name="env" value="qa"/>
<test name="LoginTests">
<classes>
<class name="com.salesinventory.tests.LoginTest"/>
<class name="com.salesinventory.tests.DashboardTest"/>
</classes>
</test>
<test name="CheckoutTests">
<classes>
<class name="com.salesinventory.tests.CheckoutTest"/>
</classes>
</test>
</suite>
πΉ (C) Reports
After execution, TestNG automatically generates reports in:
π test-output folder β
index.html(summary report)emailable-report.html(can be emailed)
π 5. How TestNG fits in Automation Framework
Selenium Tests (@Test methods)
β
TestNG Annotations (@Before/After, @Test)
β
testng.xml (Suite: smoke, regression, parallel)
β
Maven (mvn clean test -DsuiteXmlFile=regression.xml)
β
Jenkins (automated CI/CD job)
β
Reports (TestNG + Extent + Screenshots + Logs)
β In Summary
What is TestNG? β A testing framework for Java (next-gen JUnit).
Why it came? β To overcome JUnit limitations.
What problem it solves? β Grouping, parallel execution, parameterization, reporting.
Structure? β Annotations, testng.xml, Reports.
π 1. TestNG Annotations Overview
| Annotation | Description | Execution Scope |
@BeforeSuite | Runs once before the entire suite | Suite |
@AfterSuite | Runs once after the entire suite | Suite |
@BeforeTest | Runs before <test> tag in XML | Test |
@AfterTest | Runs after <test> tag in XML | Test |
@BeforeGroups | Runs before the first test method of the specified group(s) | Group |
@AfterGroups | Runs after the last test method of the specified group(s) | Group |
@BeforeClass | Runs before the first method of the current class | Class |
@AfterClass | Runs after all methods of the current class | Class |
@BeforeMethod | Runs before each @Test method | Method |
@AfterMethod | Runs after each @Test method | Method |
@Test | Marks a test case | Method |
@DataProvider | Provides data for @Test methods | Method |
@Factory | Creates dynamic instances of test classes | Class |
@Listeners | Specifies TestNG listeners for the class | Class |
@Parameters | Passes parameters from XML to tests | Method |
@Optional | Marks parameter as optional when using @Parameters | Method |
π 2. @Test Annotation β All Attributes
| Attribute | Description | Example |
priority | Order of execution (lower first) | @Test(priority=1) |
groups | Assign test to one or more groups | @Test(groups={"smoke","regression"}) |
dependsOnMethods | Test depends on other methods | @Test(dependsOnMethods={"loginTest"}) |
dependsOnGroups | Test depends on other groups | @Test(dependsOnGroups={"smoke"}) |
alwaysRun | Run test even if dependencies fail | @Test(dependsOnMethods="init", alwaysRun=true) |
enabled | Enable or disable test (default = true) | @Test(enabled=false) |
invocationCount | Number of times to run the test | @Test(invocationCount=3) |
threadPoolSize | Run multiple invocations in parallel | @Test(invocationCount=5, threadPoolSize=2) |
timeOut | Fail test if it takes longer than X ms | @Test(timeOut=2000) |
expectedExceptions | Pass test if exception occurs | @Test(expectedExceptions=ArithmeticException.class) |
description | Adds description for reports | @Test(description="Verify login functionality") |
dataProvider | Name of @DataProvider method | @Test(dataProvider="userData") |
suiteName | Assign test to a suite | @Test(suiteName="RegressionSuite") |
testName | Assign test to a test block | @Test(testName="LoginTestGroup") |
π 3. @Before/@After Annotations Attributes
| Annotation | Key Attributes | Example Usage |
@BeforeSuite / @AfterSuite | alwaysRun β run even if dependent failed | @BeforeSuite(alwaysRun=true) |
@BeforeTest / @AfterTest | alwaysRun | @BeforeTest(alwaysRun=true) |
@BeforeClass / @AfterClass | alwaysRun | @BeforeClass(alwaysRun=true) |
@BeforeMethod / @AfterMethod | alwaysRun | @BeforeMethod(alwaysRun=true) |
@BeforeGroups / @AfterGroups | groups β specify which group(s) to run before/after | @BeforeGroups(groups={"smoke"}) |
Notes:
alwaysRun=trueensures execution even if a dependent configuration fails.groupsinBeforeGroups/AfterGroupsmust match group names in@Test.
π 4. DataProvider Annotation
Supplies multiple data sets to a test.
Must return Object[][] or Iterator<Object[]>.
@DataProvider(name="loginData")
public Object[][] getData() {
return new Object[][] {
{"user1","pass1"},
{"user2","pass2"}
};
}
@Test(dataProvider="loginData")
public void loginTest(String username, String password) {
System.out.println(username + " | " + password);
}
π 5. @Parameters & @Optional
<parameter name="browser" value="chrome"/>
<parameter name="env" value="qa"/>
@Parameters({"browser", "env"})
@BeforeTest
public void setup(@Optional("chrome") String browser, String env) {
System.out.println(browser + " | " + env);
}
@Optionalprovides default value if not specified in XML.
π 6. @Factory
- Dynamically creates instances of test classes.
@Factory
public Object[] createInstances() {
return new Object[] { new TestClass(1), new TestClass(2) };
}
π 7. @Listeners
- Attach listeners to a test class for logging, reporting, or screenshots.
@Listeners(com.salesinventory.utils.ExtentReportListener.class)
public class LoginTest { }
π 8. Execution Flow of Annotations
@BeforeSuite
@BeforeTest
@BeforeGroups (if any)
@BeforeClass
@BeforeMethod
@Test
@AfterMethod
@AfterClass
@AfterGroups (if any)
@AfterTest
@AfterSuite
π 9. Summary Table β TestNG All Annotations + Attributes
| Annotation | Scope | Common Attributes |
| @BeforeSuite / @AfterSuite | Suite | alwaysRun |
| @BeforeTest / @AfterTest | Test | alwaysRun |
| @BeforeGroups / @AfterGroups | Group | groups, alwaysRun |
| @BeforeClass / @AfterClass | Class | alwaysRun |
| @BeforeMethod / @AfterMethod | Method | alwaysRun |
| @Test | Method | priority, groups, dependsOnMethods, dependsOnGroups, alwaysRun, enabled, invocationCount, threadPoolSize, timeOut, expectedExceptions, description, dataProvider, suiteName, testName |
| @DataProvider | Method | name, parallel |
| @Parameters | Method | N/A |
| @Optional | Method | value (default value) |
| @Factory | Class | N/A |
| @Listeners | Class | N/A |
β With this table, you have every TestNG annotation and attribute β fully detailed for interviews, practical automation, and CI/CD frameworks.