Skip to main content

Command Palette

Search for a command to run...

testNG -Unit Testing Framework

Published
β€’7 min readβ€’View as Markdown

πŸ“Œ 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.

AnnotationMeaningExample
@BeforeSuiteRuns before entire suiteDB connection
@AfterSuiteRuns after entire suiteClose DB connection
@BeforeTestRuns before <test> block in XMLSetup browser
@AfterTestRuns after <test> block in XMLQuit browser
@BeforeClassRuns before class methodsLogin
@AfterClassRuns after class methodsLogout
@BeforeMethodRuns before each test methodNavigate to page
@AfterMethodRuns after each test methodClear cookies
@TestActual test caseVerify 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

AnnotationDescriptionExecution Scope
@BeforeSuiteRuns once before the entire suiteSuite
@AfterSuiteRuns once after the entire suiteSuite
@BeforeTestRuns before <test> tag in XMLTest
@AfterTestRuns after <test> tag in XMLTest
@BeforeGroupsRuns before the first test method of the specified group(s)Group
@AfterGroupsRuns after the last test method of the specified group(s)Group
@BeforeClassRuns before the first method of the current classClass
@AfterClassRuns after all methods of the current classClass
@BeforeMethodRuns before each @Test methodMethod
@AfterMethodRuns after each @Test methodMethod
@TestMarks a test caseMethod
@DataProviderProvides data for @Test methodsMethod
@FactoryCreates dynamic instances of test classesClass
@ListenersSpecifies TestNG listeners for the classClass
@ParametersPasses parameters from XML to testsMethod
@OptionalMarks parameter as optional when using @ParametersMethod

πŸ“Œ 2. @Test Annotation – All Attributes

AttributeDescriptionExample
priorityOrder of execution (lower first)@Test(priority=1)
groupsAssign test to one or more groups@Test(groups={"smoke","regression"})
dependsOnMethodsTest depends on other methods@Test(dependsOnMethods={"loginTest"})
dependsOnGroupsTest depends on other groups@Test(dependsOnGroups={"smoke"})
alwaysRunRun test even if dependencies fail@Test(dependsOnMethods="init", alwaysRun=true)
enabledEnable or disable test (default = true)@Test(enabled=false)
invocationCountNumber of times to run the test@Test(invocationCount=3)
threadPoolSizeRun multiple invocations in parallel@Test(invocationCount=5, threadPoolSize=2)
timeOutFail test if it takes longer than X ms@Test(timeOut=2000)
expectedExceptionsPass test if exception occurs@Test(expectedExceptions=ArithmeticException.class)
descriptionAdds description for reports@Test(description="Verify login functionality")
dataProviderName of @DataProvider method@Test(dataProvider="userData")
suiteNameAssign test to a suite@Test(suiteName="RegressionSuite")
testNameAssign test to a test block@Test(testName="LoginTestGroup")

πŸ“Œ 3. @Before/@After Annotations Attributes

AnnotationKey AttributesExample Usage
@BeforeSuite / @AfterSuitealwaysRun β†’ run even if dependent failed@BeforeSuite(alwaysRun=true)
@BeforeTest / @AfterTestalwaysRun@BeforeTest(alwaysRun=true)
@BeforeClass / @AfterClassalwaysRun@BeforeClass(alwaysRun=true)
@BeforeMethod / @AfterMethodalwaysRun@BeforeMethod(alwaysRun=true)
@BeforeGroups / @AfterGroupsgroups β†’ specify which group(s) to run before/after@BeforeGroups(groups={"smoke"})

Notes:

  • alwaysRun=true ensures execution even if a dependent configuration fails.

  • groups in BeforeGroups/AfterGroups must 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);
}
  • @Optional provides 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

AnnotationScopeCommon Attributes
@BeforeSuite / @AfterSuiteSuitealwaysRun
@BeforeTest / @AfterTestTestalwaysRun
@BeforeGroups / @AfterGroupsGroupgroups, alwaysRun
@BeforeClass / @AfterClassClassalwaysRun
@BeforeMethod / @AfterMethodMethodalwaysRun
@TestMethodpriority, groups, dependsOnMethods, dependsOnGroups, alwaysRun, enabled, invocationCount, threadPoolSize, timeOut, expectedExceptions, description, dataProvider, suiteName, testName
@DataProviderMethodname, parallel
@ParametersMethodN/A
@OptionalMethodvalue (default value)
@FactoryClassN/A
@ListenersClassN/A

βœ… With this table, you have every TestNG annotation and attribute β€” fully detailed for interviews, practical automation, and CI/CD frameworks.