TestNG Interview Questions & Answers
1. What is TestNG?
TestNG (Test Next Generation) is a testing framework inspired by JUnit and NUnit but designed to be more powerful and flexible. It supports annotations, parallel execution, data-driven testing, and reporting.
2. What are the main features of TestNG?
- Annotation-based testing
- Parallel test execution
- Data-driven testing using DataProvider
- Flexible test configuration
- Dependency management
- Detailed reporting
3. What are TestNG annotations?
Annotations define how methods should behave in a test. Common ones include:
- @Test – marks a test method
- @BeforeMethod / @AfterMethod
- @BeforeClass / @AfterClass
- @BeforeSuite / @AfterSuite
4. What is the difference between @BeforeMethod and @BeforeClass?
- @BeforeMethod: runs before each test method
- @BeforeClass: runs once before all test methods in a class
5. What is the difference between TestNG and JUnit?
| TestNG | JUnit |
| Supports parallel execution | Limited parallel support |
| Supports dependency testing | No dependency feature |
| Supports DataProvider | Limited parameterization |
| More flexible annotations | Fewer annotations |
| Better reporting | Basic reporting |
6. What is testng.xml file?
testng.xml is a configuration file used to control test execution in TestNG.
It helps to:
- Define test suites
- Group tests
- Run tests in parallel
- Pass parameters
- Control execution order
Example:
<suite name="Suite">
<test name="Test">
<classes>
<class name="tests.LoginTest"/>
</classes>
</test>
</suite
7. What are groups in TestNG?
Groups are used to categorize test methods.
Example:
@Test(groups = "smoke")
public void loginTest() {
}
You can run only specific groups using testng.xml.
8. What is the use of dependsOnGroups in TestNG?
It allows a test method to depend on an entire group of tests.
Example:
Groups are used to categorize test methods.
Example:
@Test(groups = "smoke")
public void loginTest() {
}
You can run only specific groups using testng.xml.
9. What is dependency in TestNG?
Dependency means one test method depends on another test method.
Example:
@Test
public void login() {
}
@Test(dependsOnMethods = "login")
public void dashboard() {
}
If login() fails, dashboard() will be skipped.
10. What is the difference between Hard Assert and Soft Assert?
Hard Assert
- Stops execution immediately if assertion fails.
Assert.assertEquals(actual, expected);
Soft Assert
- Continues execution even after assertion failure.
SoftAssert sa = new SoftAssert();
sa.assertEquals(actual, expected);
sa.assertAll();
11. What is assertion in TestNG?
Assertions are used to validate expected and actual results.
Common assertions:
- assertEquals()
- assertTrue()
- assertFalse()
- assertNull()
12. What is parameterization in TestNG?
Parameterization allows passing values to test methods from testng.xml.
Example:
<parameter name="browser" value="chrome"/>
@Parameters("browser")
@Test
public void launch(String browser) {
}
13. What is the difference between Parameters and DataProvider?
| Parameters | DataProvider |
| Values from XML | Values from Java method |
| Used for simple data | Used for multiple datasets |
| Less flexible | More flexible |
14. How can you run tests in parallel in TestNG?
Using parallel attribute in testng.xml.
Example:
<suite name="Suite" parallel="tests" thread-count="2">
15. What is invocationCount in TestNG?
It is used to run a test multiple times.
Example:
@Test(invocationCount = 5)
public void testMethod() {
}
16. What is priority in TestNG?
Priority controls execution order of test methods.
Example:
@Test(priority = 1)
public void login() {
}
@Test(priority = 2)
public void logout() {
}
Lower priority executes first.
17. What happens if priority is not set?
Test methods execute in alphabetical order by method name.
18. What is timeout in TestNG?
Timeout defines maximum execution time for a test.
Example:
@Test(timeOut = 3000)
public void test() {
}
Test fails if execution exceeds 3 seconds.
19. What is enabled=false in TestNG?
Used to skip a test method.
Example:
@Test(enabled = false)
public void skipTest() {
}
20. What is the difference between @BeforeTest and @BeforeSuite?
| @BeforeTest | @BeforeSuite |
| Runs before test tag | Runs before entire suite |
| Executes multiple times | Executes once |
21. How do you skip a test in TestNG?
Two ways:
- enabled = false
- Using SkipException
Example:
throw new SkipException("Skipping test")
22. What are listeners in TestNG?
Listeners are interfaces that modify TestNG behavior during execution.
Common listeners:
- ITestListener
- ISuiteListener
Used for:
- Logging
- Screenshots
- Reporting
23. What is ITestListener?
It is a listener interface used to track test execution events.
Methods include:
- onTestStart()
- onTestSuccess()
- onTestFailure()
24. How do you generate reports in TestNG?
TestNG automatically generates reports inside the test-output folder.
Main reports:
- index.html
- emailable-report.html
25. What is factory annotation in TestNG?
@Factory is used to create multiple instances of test classes dynamically.
26. What is the difference between @BeforeTest and @BeforeMethod?
| @BeforeTest | @BeforeMethod |
| Runs before all test methods in a test | Runs before each test method |
| Executes once | Executes multiple times |
27. Can TestNG be integrated with Selenium?
Yes. TestNG is widely used with Selenium for:
- Test execution
- Reporting
- Assertions
- Parallel testing
- Data-driven testing
28. How do you retry failed test cases in TestNG?
Using IRetryAnalyzer.
Example:
public class Retry implements IRetryAnalyzer {
int count = 0;
public boolean retry(ITestResult result) {
if(count < 2) {
count++;
return true;
}
return false;
}
}
29. What is alwaysRun=true in TestNG?
It ensures the method executes even if dependent methods fail.
Example:
@Test(alwaysRun = true)
30. What is thread-count in TestNG?
It defines the number of threads used during parallel execution.
Example:
thread-count="3"