</>

Technology

TestNG

Difficulty

Advanced

Interview Question

What is ITestContext in TestNG and how do you use it?

ITestContext represents a TestNG test run context — use it to share data between test methods, access suite/test attributes, and read configuration from testng.xml.

Answer

ITestContext in TestNG

ITestContext gives access to the current test run context — including attributes, parameters, included/excluded groups, and test results. It is the primary mechanism for sharing data between tests without static variables.

Access ITestContext in @Before/@After Methods

Java
@BeforeClass
public void setUp(ITestContext context) {
    // Set shared attributes accessible by any test in this class
    context.setAttribute("baseUrl", "https://staging.automateqa.online");
    context.setAttribute("authToken", login());
}

@Test
public void apiTest(ITestContext context) {
    String baseUrl = (String) context.getAttribute("baseUrl");
    String token   = (String) context.getAttribute("authToken");
    // Use shared data without static variables
}

Read testng.xml Parameters

Java
@BeforeClass
public void configure(ITestContext context) {
    // Access <parameter name="env" value="staging"/> from testng.xml
    String env = context.getCurrentXmlTest().getParameter("env");
    System.out.println("Running on: " + env);
}

Access Test Results During Execution

Java
@AfterClass
public void printResults(ITestContext context) {
    System.out.println("Passed:  " + context.getPassedTests().size());
    System.out.println("Failed:  " + context.getFailedTests().size());
    System.out.println("Skipped: " + context.getSkippedTests().size());

    // Get all failed test names
    context.getFailedTests().getAllResults().forEach(result ->
        System.out.println("Failed: " + result.getName())
    );
}

Share Driver Between Tests (Not Recommended but Common Pattern)

Java
@BeforeClass
public void setUp(ITestContext context) {
    WebDriver driver = new ChromeDriver();
    context.setAttribute("driver", driver); // share across methods
}

@Test
public void test1(ITestContext context) {
    WebDriver driver = (WebDriver) context.getAttribute("driver");
    driver.get("https://example.com");
}

@AfterClass
public void tearDown(ITestContext context) {
    ((WebDriver) context.getAttribute("driver")).quit();
}

ITestContext in Listeners

Java
public class CustomListener implements ITestListener {

    @Override
    public void onStart(ITestContext context) {
        System.out.println("Suite: " + context.getSuite().getName());
        System.out.println("Test:  " + context.getName());
        System.out.println("Groups: " + Arrays.toString(context.getIncludedGroups()));
    }

    @Override
    public void onFinish(ITestContext context) {
        int total = context.getPassedTests().size()
                  + context.getFailedTests().size()
                  + context.getSkippedTests().size();
        System.out.println("Execution complete. Total: " + total);
    }
}

ITestContext vs ITestResult

InterfaceScopeUse For
ITestContextEntire test (<test> tag in XML)Shared data, suite-level info, results summary
ITestResultSingle test methodPass/fail status, exceptions, method name
ISuiteEntire suiteSuite-level attributes, all tests

Follow AutomateQA

Related Topics