In the world of automation testing, TestNG is one of the most widely used testing frameworks for Java. Built as an enhanced version of JUnit, TestNG provides advanced features such as annotations, parallel execution, parameterization, and detailed reporting.
For QA engineers, automation testers, and developers, TestNG often appears as a common topic in technical interviews. Having a solid understanding of TestNG not only helps you clear interviews but also boosts your confidence when working on real-world automation projects.
This article compiles the top 20 TestNG interview questions with examples, helping you prepare effectively for your next interview.
1. What is TestNG?
Answer:
TestNG (Test Next Generation) is a testing framework inspired by JUnit and NUnit but with additional features like annotations, grouping, parallel testing, and flexible test configuration.
Example:
import org.testng.annotations.Test;
public class SampleTest {
@Test
public void testCase() {
System.out.println("This is a TestNG test case.");
}
}
2. What are the advantages of TestNG over JUnit?
Answer:
- Supports annotations (@Test, @BeforeTest, etc.)
- Allows parallel execution of tests
- Provides HTML and XML reports
- Flexible configuration with testng.xml
- Supports data-driven testing using @DataProvider
3. What is the use of @Test annotation in TestNG?
Answer:
The @Test
annotation marks a method as a test case.
Example:
@Test
public void loginTest() {
System.out.println("Login functionality test");
}
4. What is the role of @BeforeSuite, @BeforeTest, @BeforeClass, and @BeforeMethod?
Answer:
- @BeforeSuite: Runs before all tests in the suite.
- @BeforeTest: Runs before test tags in XML.
- @BeforeClass: Runs before the first method in the class.
- @BeforeMethod: Runs before each test method.
Example:
@BeforeSuite
public void setupSuite() {
System.out.println("Suite setup");
}
5. What is testng.xml in TestNG?
Answer:
The testng.xml
file is used to configure test cases, group execution, parameterization, and parallel execution.
Example:
<suite name="Suite1">
<test name="Test1">
<classes>
<class name="com.qa.SampleTest"/>
</classes>
</test>
</suite>
6. How do you perform parameterization in TestNG?
Answer:
TestNG supports parameterization using @Parameters
or @DataProvider
.
Example with @Parameters:
@Test
@Parameters("username")
public void loginTest(String user) {
System.out.println("Logging in with user: " + user);
}
7. What is @DataProvider in TestNG?
Answer:@DataProvider
supplies multiple sets of data to a single test method.
Example:
@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("Login with: " + username + ", " + password);
}
8. How do you run tests in parallel in TestNG?
Answer:
Use the parallel
attribute in testng.xml
.
Example:
<suite name="Suite" parallel="tests" thread-count="2">
<test name="Test1">
<classes>
<class name="com.qa.SampleTest1"/>
</classes>
</test>
<test name="Test2">
<classes>
<class name="com.qa.SampleTest2"/>
</classes>
</test>
</suite>
9. What is test grouping in TestNG?
Answer:
Groups allow execution of a subset of tests.
Example:
@Test(groups = {"smoke"})
public void smokeTest() {
System.out.println("Smoke test executed");
}
10. How do you disable a test in TestNG?
Answer:
Set enabled=false
in the @Test
annotation.
Example:
@Test(enabled=false)
public void skipTest() {
System.out.println("This will not run.");
}
11. How do you prioritize tests in TestNG?
Answer:
Use the priority
attribute in @Test
.
Example:
@Test(priority=1)
public void testA() {}
@Test(priority=2)
public void testB() {}
12. How do you handle dependencies in TestNG?
Answer:
Use dependsOnMethods
.
Example:
@Test
public void login() {}
@Test(dependsOnMethods={"login"})
public void purchaseItem() {}
13. How does TestNG generate reports?
Answer:
After execution, TestNG generates reports in the test-output
folder, including HTML and XML reports.
14. How do you retry failed tests in TestNG?
Answer:
Implement IRetryAnalyzer
.
Example:
public class RetryAnalyzer implements IRetryAnalyzer {
int count = 0;
int maxTry = 2;
public boolean retry(ITestResult result) {
if(count < maxTry) {
count++;
return true;
}
return false;
}
}
15. How do you use listeners in TestNG?
Answer:
Listeners like ITestListener
help customize reporting and logging.
Example:
public class TestListener implements ITestListener {
public void onTestSuccess(ITestResult result) {
System.out.println("Test Passed: " + result.getName());
}
}
16. What is Soft Assert in TestNG?
Answer:
Unlike hard assertions, soft assertions allow execution to continue even after failures.
Example:
SoftAssert softAssert = new SoftAssert();
softAssert.assertEquals("Hello", "Hi");
softAssert.assertAll();
17. How do you skip tests programmatically in TestNG?
Answer:
Throw a SkipException
.
Example:
@Test
public void skipTest() {
throw new SkipException("Skipping this test");
}
18. What is parallel execution at method level?
Answer:
Set parallel="methods"
in testng.xml
. This runs multiple methods concurrently.
19. How do you integrate TestNG with Maven?
Answer:
Add the TestNG dependency in pom.xml
:
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.8.0</version>
<scope>test</scope>
</dependency>
20. How do you integrate TestNG with Selenium?
Answer:
TestNG is often used with Selenium for automation testing.
Example:
@Test
public void openBrowser() {
WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
driver.quit();
}
Conclusion
TestNG is a powerful testing framework that goes beyond JUnit by supporting annotations, parallel execution, grouping, data providers, and advanced reporting. By preparing for these 20 TestNG interview questions with examples, you can confidently handle both beginner and advanced interview scenarios.
Whether you are an automation tester working with Selenium, cloud-based test pipelines, or enterprise DevOps environments, mastering TestNG is essential for career growth.
Call to Action: Start practicing these examples in your IDE today and integrate them with Selenium or Maven to strengthen your automation skills.
FAQs
Q1. Is TestNG better than JUnit?
Yes, TestNG offers more advanced features such as grouping, parallel execution, and flexible configuration.
Q2. Can TestNG be used with Selenium?
Yes, TestNG is widely used with Selenium WebDriver for automation testing.
Q3. What is @DataProvider in TestNG?@DataProvider
allows you to run the same test multiple times with different sets of data.
Q4. How do I run only smoke tests in TestNG?
You can use groups (@Test(groups="smoke")
) and configure them in testng.xml
.
Q5. Does TestNG support parallel execution?
Yes, you can configure parallel execution at test, class, or method level.