✅ 1. What is TestNG?
Answer:
TestNG (Test Next Generation) is a testing framework inspired by JUnit and NUnit. It is widely used in Selenium automation for:
-
Managing test cases.
-
Setting test priorities.
-
Grouping and sequencing tests.
-
Generating test reports.
It provides features like @Test, @BeforeMethod, @AfterMethod, @DataProvider, parallel execution, and more.
✅ 2. What are the advantages of using TestNG?
Answer:
-
Annotations are easier to understand.
-
Parallel test execution.
-
Detailed HTML test reports.
-
Support for grouping and prioritizing test cases.
-
Data-driven testing using
@DataProvider. -
Dependency and suite-level configuration support.
✅ 3. What are TestNG annotations?
Answer:
Annotations in TestNG control test flow. Common ones:
| Annotation | Purpose |
|---|---|
@Test |
Marks a method as a test case |
@BeforeMethod |
Runs before each @Test method |
@AfterMethod |
Runs after each @Test method |
@BeforeClass |
Runs once before all tests in a class |
@AfterClass |
Runs once after all tests in a class |
@BeforeSuite |
Runs before any tests in the suite |
@AfterSuite |
Runs after all tests in the suite |
✅ 4. What is the difference between @BeforeMethod and @BeforeClass?
Answer:
-
@BeforeMethod: Runs before every test method. -
@BeforeClass: Runs once before any test method in the class.
Use @BeforeMethod to open browsers or login, and @BeforeClass for setting up heavy configurations.
✅ 5. How do you prioritize tests in TestNG?
Answer:
Using the priority attribute in the @Test annotation.
@Test(priority = 1)public void loginTest() { }@Test(priority = 2)public void searchTest() { }
Tests with lower priority numbers execute first.
✅ 6. How do you skip a test in TestNG?
Answer:
You can skip a test by:
-
Setting
enabled = false:
@Test(enabled = false)public void skipTest() { }
-
Using
throw new SkipException("...")inside the method.
✅ 7. What is testng.xml?
Answer:
testng.xml is a configuration file used to define test suites, include/exclude classes or methods, run groups, set parallel execution, and pass parameters.
Example:
<suite name="MySuite"><test name="MyTest"><classes><class name="tests.LoginTest" /></classes></test></suite>
✅ 8. How do you execute only selected test cases using testng.xml?
Answer:
Use <include> or <exclude> inside the <methods> tag.
<methods><include name="loginTest"/><exclude name="logoutTest"/></methods>
✅ 9. How do you group tests in TestNG?
Answer:
Use the groups attribute:
@Test(groups = "smoke")public void loginTest() { }@Test(groups = {"smoke", "regression"})public void dashboardTest() { }
In testng.xml:
<groups><run><include name="smoke"/></run></groups>
✅ 10. What is @DataProvider in TestNG?
Answer:
Used for data-driven testing by passing multiple sets of data to a single test method.
Example:
@DataProviderpublic Object[][] loginData() {return new Object[][] {{"admin", "pass1"},{"user", "pass2"}};}@Test(dataProvider = "loginData")public void testLogin(String user, String pass) {// login test logic}
✅ 11. What is a test suite?
Answer:
A test suite is a collection of test cases or test classes grouped and run together. Defined in testng.xml.
✅ 12. How do you run tests in parallel in TestNG?
Answer:
In testng.xml:
<suite name="Suite" parallel="tests" thread-count="2">...</suite>
Or using parallel attributes like parallel="classes" or methods.
✅ 13. What is the difference between soft and hard assertions?
Answer:
-
Hard Assert: Fails immediately if the condition is false.
-
Soft Assert: Collects all assertions and reports them at the end using
assertAll().
Example:
SoftAssert sa = new SoftAssert();sa.assertEquals(1, 2);sa.assertAll(); // now reports the failure
✅ 14. How do you pass parameters from testng.xml to test methods?
Answer:
Use @Parameters annotation.
<parameter name="browser" value="chrome"/>
In code:
@Parameters("browser")@Testpublic void testBrowser(String browserName) {System.out.println(browserName);}
✅ 15. How do you generate reports in TestNG?
Answer:
TestNG generates default HTML and XML reports automatically inside the test-output folder. You can also use third-party tools like ExtentReports for advanced reporting.
✅ 16. What is dependsOnMethods in TestNG?
Answer:
Used to define test dependencies.
@Testpublic void login() { }@Test(dependsOnMethods = "login")public void dashboard() { }
If login fails, dashboard is skipped.
✅ 17. What is the sequence of TestNG annotations?
Answer:
@BeforeSuite@BeforeTest@BeforeClass@BeforeMethod@Test@AfterMethod@AfterClass@AfterTest@AfterSuite
✅ 18. Can we run TestNG without testng.xml?
Answer:
Yes. You can directly run classes with @Test methods from your IDE or using Maven/Gradle. However, testng.xml is needed for:
-
Grouping
-
Parallel execution
-
Configurations
✅ 19. What is InvocationCount in TestNG?
Answer:
It allows a test method to run multiple times.
@Test(invocationCount = 3)public void repeatTest() {System.out.println("Run again");}
✅ 20. How do you ignore a test method in TestNG?
Answer:
Use enabled = false in the @Test annotation:
@Test(enabled = false)public void skippedTest() { }
✅ 21. What is timeout in TestNG?
Answer:
The timeout attribute in TestNG specifies the maximum time a test method is allowed to run.
If the test takes longer, it fails.
@Test(timeOut = 2000) // 2 secondspublic void slowTest() throws InterruptedException {Thread.sleep(3000); // This will cause the test to fail}
✅ 22. What is alwaysRun in TestNG?
Answer:
alwaysRun = true ensures that the test method will run even if its dependent methods fail.
@Testpublic void login() {Assert.fail();}@Test(dependsOnMethods = "login", alwaysRun = true)public void dashboard() {System.out.println("Runs anyway");}
✅ 23. What is parameterization in TestNG?
Answer:
Parameterization is the process of passing data to test methods from external sources, such as:
-
testng.xml(using@Parameters) -
@DataProvider
It allows reusability of test logic with different inputs.
✅ 24. Can we have multiple @DataProvider methods in a class?
Answer:
Yes. You can define multiple @DataProvider methods in the same or different classes and use them via the dataProvider attribute.
@DataProvider(name = "data1")public Object[][] dp1() { return new Object[][] { ... }; }@DataProvider(name = "data2")public Object[][] dp2() { return new Object[][] { ... }; }
Use them:
@Test(dataProvider = "data2")public void testX(...) { ... }
✅ 25. How to use a DataProvider from another class?
Answer:
-
Create a separate class with your
@DataProvidermethod. -
Reference it using
dataProviderClass.
Example:
DataProvider class:
public class DataClass {@DataProvider(name = "loginData")public Object[][] getData() { return ...; }}
Test class:
@Test(dataProvider = "loginData", dataProviderClass = DataClass.class)public void testLogin(String user, String pass) { ... }
✅ 26. What is invocationCount used for?
Answer:
Used to run the same test method multiple times automatically.
@Test(invocationCount = 5)public void runMultipleTimes() {System.out.println("Running again");}
✅ 27. How do you create test dependencies in TestNG?
Answer:
Use the dependsOnMethods or dependsOnGroups attribute.
@Testpublic void openApp() {}@Test(dependsOnMethods = "openApp")public void login() {}
This makes login() run only if openApp() passes.
✅ 28. Can we have @Test methods without @BeforeMethod or @AfterMethod?
Answer:
Yes, @Test methods can run independently. @BeforeMethod and @AfterMethod are optional and only used for setting pre- and post-test conditions.
✅ 29. What is parallel execution in TestNG?
Answer:
TestNG supports running tests in parallel using the parallel attribute in testng.xml. It saves execution time.
Types:
-
parallel="tests" -
parallel="classes" -
parallel="methods"
Example:
<suite name="Suite" parallel="methods" thread-count="3">
✅ 30. Can you pass parameters from command line in TestNG?
Answer:
Yes, using Maven's -D syntax or Jenkins build parameters with @Parameters in code.
Example:
Command:
mvn test -Dbrowser=chrome
Code:
@Parameters("browser")@Testpublic void launch(String browser) { ... }
✅ 31. What is the difference between @Factory and @DataProvider?
Answer:
-
@DataProvider: Runs the same test method with different data. -
@Factory: Runs different test instances, each created with constructor arguments.
@Factory is used when you need different object creation logic for each test.
✅ 32. What is ITestListener in TestNG?
Answer:
ITestListener is an interface used to monitor test execution status. You can override its methods to take actions on:
-
test success
-
test failure
-
test skip
-
test start and finish
Example use: capture screenshot on test failure.
✅ 33. How do you implement ITestListener?
Answer:
-
Create a class that implements
ITestListener. -
Override methods like
onTestFailure().
public class MyListener implements ITestListener {public void onTestFailure(ITestResult result) {System.out.println("Failed: " + result.getName());}}
-
Add the listener:
@Listeners(MyListener.class)public class TestClass { ... }
✅ 34. What is IReporter in TestNG?
Answer:
IReporter is an interface that lets you create custom reports after the test suite execution is complete.
You override:
generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory)
✅ 35. What is ISuiteListener?
Answer:
ISuiteListener lets you perform actions before and after a suite is run.
public class MySuiteListener implements ISuiteListener {public void onStart(ISuite suite) { ... }public void onFinish(ISuite suite) { ... }}
Useful for starting/stopping servers or initializing suite-wide data.
✅ 36. What is thread safety in TestNG?
Answer:
Thread safety ensures that shared resources used in parallel execution do not cause unexpected behavior. You must:
-
Avoid static/global variables.
-
Use
ThreadLocalor synchronized blocks if needed. -
Use proper test data isolation per thread.
✅ 37. How to create a base test class in TestNG?
Answer:
A BaseTest class helps to avoid code repetition:
public class BaseTest {WebDriver driver;@BeforeMethodpublic void setUp() { driver = new ChromeDriver(); }@AfterMethodpublic void tearDown() { driver.quit(); }}
Then extend this class:
public class LoginTest extends BaseTest {@Testpublic void login() { ... }}
✅ 38. Can you run failed tests again in TestNG?
Answer:
Yes. TestNG automatically creates a testng-failed.xml in the test-output folder, which includes only failed test cases.
You can re-run this file to retry failed tests.
✅ 39. How do you rerun failed tests using listeners?
Answer:
Implement IRetryAnalyzer:
public class RetryTest implements IRetryAnalyzer {int count = 0;public boolean retry(ITestResult result) {if (count < 2) {count++;return true;}return false;}}
Attach it to your test:
@Test(retryAnalyzer = RetryTest.class)public void flakyTest() { ... }
✅ 40. What is the default order of execution of test methods in TestNG?
Answer:
By default, TestNG runs test methods in alphabetical order, unless you set a priority.
To control order explicitly, use:
@Test(priority = 1)public void aTest() { }@Test(priority = 2)public void bTest() { }
✅ 41. What is the purpose of @BeforeTest and @AfterTest?
Answer:
-
@BeforeTest: Runs once before any test methods in a<test>tag intestng.xml. -
@AfterTest: Runs once after all test methods in that<test>tag.
These are useful for setting up or tearing down test-level configurations such as:
-
Connecting to a database.
-
Starting/stopping a server.
-
Loading common resources.
✅ 42. Can we run only one specific test method using TestNG?
Answer:
Yes. You can:
-
Use
testng.xmlwith<include name="methodName" />to include a single method. -
Right-click and run the test method directly in your IDE (e.g., Eclipse, IntelliJ).
-
Use groups or filters to isolate test execution.
✅ 43. What happens if we don’t use priority in @Test methods?
Answer:
If priority is not defined, TestNG runs test methods in lexicographical (alphabetical) order of method names.
Example:
@Testpublic void bTest() {}@Testpublic void aTest() {}
Here, aTest() will run before bTest().
✅ 44. What is the use of testng.xml file in Selenium framework?
Answer:
testng.xml helps in:
-
Organizing test execution flow.
-
Grouping and running multiple test classes/suites.
-
Running tests in parallel.
-
Passing parameters.
-
Including/excluding specific test methods.
It's the central controller for running the test suite in a structured and configurable way.
✅ 45. Can we run TestNG tests using Maven?
Answer:
Yes. You can run TestNG tests using the Maven Surefire plugin.
Steps:
-
Add TestNG and Surefire plugin in
pom.xml.
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>3.0.0</version><configuration><suiteXmlFiles><suiteXmlFile>testng.xml</suiteXmlFile></suiteXmlFiles></configuration></plugin>
-
Run tests:
mvn clean test
✅ 46. Can we have multiple testng.xml files in one project?
Answer:
Yes. You can create multiple testng.xml files to:
-
Run different sets of tests.
-
Separate smoke, regression, UAT tests.
-
Manage browser-specific suites.
Each XML can be run individually or merged into a master suite.
✅ 47. How do you ignore a group of tests in TestNG?
Answer:
In testng.xml, exclude the group using:
<groups><run><exclude name="sanity"/></run></groups>
Or in code:
@Test(groups = "sanity", enabled = false)public void testA() {}
✅ 48. Can @DataProvider return different data types?
Answer:
Yes. @DataProvider returns an Object[][], which can contain different data types in each column.
@DataProviderpublic Object[][] mixedData() {return new Object[][] {{"admin", 123, true},{"guest", 456, false}};}
The receiving test method must match parameter types:
@Test(dataProvider = "mixedData")public void test(String username, int id, boolean status) { ... }
✅ 49. What happens if @Test method throws an exception?
Answer:
If a test throws an unhandled exception, it will fail.
To expect exceptions, use:
@Test(expectedExceptions = ArithmeticException.class)public void testDivision() {int result = 1 / 0;}
This test will pass because the exception was expected.
✅ 50. Can we run TestNG tests in parallel on different browsers?
Answer:
Yes, this is commonly done in Selenium Grid or Cross-Browser frameworks.
Steps:
-
Use
@Parametersor@DataProviderto pass browser names. -
Set parallel execution in
testng.xml:
<suite name="CrossBrowserSuite" parallel="tests" thread-count="2"><test name="ChromeTest"><parameter name="browser" value="chrome" /><classes><class name="tests.LoginTest" /></classes></test><test name="FirefoxTest"><parameter name="browser" value="firefox" /><classes><class name="tests.LoginTest" /></classes></test></suite>
✅ 51. How does @Listeners work in TestNG?
Answer:
@Listeners attaches custom listeners (like ITestListener, ISuiteListener) to a test class to monitor and act on events like test pass, fail, skip, etc.
Usage:
@Listeners(MyListener.class)public class TestClass {@Testpublic void testA() { ... }}
Listeners help in:
-
Logging.
-
Custom report generation.
-
Screenshot capturing on failure.
✅ 52. Can we call one test method from another in TestNG?
Answer:
Technically yes, you can call one test method from another like a normal method. But it is not recommended because:
-
It breaks the independence of test cases.
-
It makes result tracking inaccurate.
Better to use:
@Test(dependsOnMethods = "methodA")
✅ 53. Can we define test methods inside @BeforeMethod?
Answer:
No. You cannot write a @Test method inside a @BeforeMethod or any other annotation method.
All @Test methods must be defined as independent public methods in the class.
✅ 54. How do you set execution order across multiple classes?
Answer:
Control execution sequence using testng.xml:
<classes><class name="tests.LoginTest" /><class name="tests.SearchTest" /><class name="tests.PaymentTest" /></classes>
TestNG will run them in this defined order unless parallel execution is enabled.
✅ 55. Can you disable an entire test class in TestNG?
Answer:
Yes. Use @Test(enabled = false) on the class level:
@Test(enabled = false)public class DisabledTests {@Testpublic void testA() { ... }}
None of the test methods in the class will run.
✅ 56. What is a suite-level parameter in TestNG?
Answer:
A suite-level parameter is declared in the <suite> tag in testng.xml and can be used across multiple tests.
<suite name="Suite"><parameter name="url" value="https://demoapp.com" />
Used in code:
@Parameters("url")@Testpublic void launchApp(String url) {System.out.println("Opening " + url);}
✅ 57. What is the scope of @BeforeSuite and @AfterSuite?
Answer:
-
@BeforeSuite: Runs once before all test cases in the entire suite. -
@AfterSuite: Runs once after all test cases are completed.
Good for:
-
Initializing suite-wide resources like DB connections or report setup.
-
Cleaning up external services after tests.
✅ 58. Can we use @Test without public keyword?
Answer:
No. In TestNG, all test methods must be public. If you write a @Test method as private/protected, TestNG will ignore it silently.
✅ 59. What are soft assertions and how to use them?
Answer:
Soft assertions allow test execution to continue even if an assertion fails, and collect all errors to report at the end.
Usage:
SoftAssert soft = new SoftAssert();soft.assertEquals(actual, expected);soft.assertTrue(condition);// Collect allsoft.assertAll(); // This triggers the failure report
✅ 60. What are factory tests in TestNG?
Answer:
@Factory is used to run multiple test instances with different constructor parameters.
Example:
public class LoginTest {private String username;public LoginTest(String username) {this.username = username;}@Testpublic void login() {System.out.println("Logging in as " + username);}}
public class TestFactory {@Factorypublic Object[] createInstances() {return new Object[] {new LoginTest("admin"),new LoginTest("guest")};}}
✅ 61. What is dependsOnGroups in TestNG?
Answer:
dependsOnGroups is used when a test method depends on an entire group of tests to pass before it runs.
Example:
@Test(groups = "login")public void loginTest() { ... }@Test(dependsOnGroups = "login")public void dashboardTest() { ... }
Here, dashboardTest() will only run after all methods in the login group pass.
✅ 62. How do you prioritize tests in TestNG?
Answer:
Use the priority attribute in @Test annotation.
Lower priority number = higher execution order.
@Test(priority = 1)public void login() {}@Test(priority = 2)public void addItem() {}
Note: If multiple methods have the same priority, they execute in alphabetical order.
✅ 63. What is the default priority in TestNG?
Answer:
The default priority is 0 if not specified.
@Testpublic void aTest() {} // default priority = 0@Test(priority = 1)public void bTest() {}
So aTest() will execute before bTest().
✅ 64. What is the difference between @BeforeMethod and @BeforeClass?
Answer:
| Annotation | Executes... |
|---|---|
@BeforeMethod |
Before each test method in the class |
@BeforeClass |
Once before the first test method in class |
Use @BeforeMethod for repeated setup (e.g., login) and @BeforeClass for one-time setup (e.g., browser launch).
✅ 65. Can @BeforeMethod and @AfterMethod be used with @DataProvider?
Answer:
Yes. If a test uses @DataProvider, @BeforeMethod and @AfterMethod will run before and after each iteration of the test.
Example:
@DataProviderpublic Object[][] loginData() {return new Object[][] {{"admin", "admin123"},{"guest", "guest123"}};}@BeforeMethodpublic void setup() {System.out.println("Setup");}@AfterMethodpublic void tearDown() {System.out.println("Teardown");}@Test(dataProvider = "loginData")public void loginTest(String user, String pass) {System.out.println("Testing with " + user);}
Output: Setup → test 1 → Teardown → Setup → test 2 → Teardown
✅ 66. Can @DataProvider be in a different class?
Answer:
Yes. Use dataProviderClass to refer to the class containing the data provider.
// In separate classpublic class DataUtils {@DataProvider(name = "loginData")public static Object[][] getLoginData() {return new Object[][] {{"admin", "admin123"},{"user", "user123"}};}}
In test class:
@Test(dataProvider = "loginData", dataProviderClass = DataUtils.class)public void loginTest(String user, String pass) { ... }
✅ 67. Can we run specific test methods from the command line?
Answer:
Yes. You can use:
java -cp "path/to/libs/*" org.testng.TestNG testng.xml
Or specify a test method using the -methods option:
java org.testng.TestNG -testclass com.tests.LoginTest -methods loginTest
Maven example:
mvn test -Dtest=LoginTest#loginTest
✅ 68. Can @Test methods be overloaded?
Answer:
Yes, but TestNG will only recognize one of them unless you specify @Test for each uniquely.
@Testpublic void testLogin(String user) { ... }@Testpublic void testLogin(String user, String pass) { ... } // Invalid: both have same name// Better:@Testpublic void loginWithUsername() { ... }@Testpublic void loginWithCredentials() { ... }
It's better to keep test method names unique and clear.
✅ 69. What is the use of InvocationCount?
Answer:
invocationCount runs the same test multiple times.
@Test(invocationCount = 3)public void retryTest() {System.out.println("Run");}
Output:
RunRunRun
Useful for load testing or flaky tests.
✅ 70. What is ThreadPoolSize in TestNG?
Answer:
It specifies how many threads to use when running tests in parallel.
Example:
@Test(invocationCount = 5, threadPoolSize = 2)public void testThreadPool() {System.out.println("Thread: " + Thread.currentThread().getId());}
Here, 5 executions will run using 2 threads, improving speed in parallel testing.
✅ 71. Can we have multiple @DataProvider in the same class?
Answer:
Yes. You can have multiple @DataProvider methods as long as each has a unique name.
@DataProvider(name = "loginData")public Object[][] login() { ... }@DataProvider(name = "signupData")public Object[][] signup() { ... }
Then refer them using dataProvider = "signupData" or loginData.
✅ 72. Can we skip a test at runtime?
Answer:
Yes. You can programmatically skip a test using:
throw new SkipException("Skipping this test");
This is useful when:
-
A feature is not ready.
-
Prerequisite condition is not met.
✅ 73. What is the purpose of ITestListener?
Answer:
ITestListener is an interface that allows you to hook into test events like:
-
onTestStart
-
onTestSuccess
-
onTestFailure
-
onTestSkipped
Example use cases:
-
Take screenshots on failure.
-
Send email alerts.
-
Custom logging.
✅ 74. What is the difference between @Factory and @DataProvider?
Answer:
| Feature | @Factory | @DataProvider |
|---|---|---|
| Return type | Object[] of test class instances | Object[][] of test data |
| Used with | Constructors | Test methods |
| Purpose | Run tests with different objects | Run tests with different data |
✅ 75. Can we define @Test in an abstract class?
Answer:
Yes, but the concrete subclass must extend it and implement/execute it.
public abstract class BaseTest {@Testpublic void commonTest() {System.out.println("Common test");}}public class MyTest extends BaseTest { }
commonTest() will be executed as part of MyTest.
✅ 76. Can we parameterize tests using Excel or CSV?
Answer:
Yes, but TestNG doesn’t support it natively.
You can:
-
Use Apache POI to read Excel.
-
Pass the data to
@DataProvider.
Example:
@DataProviderpublic Object[][] excelData() {// read Excel using Apache POI}
Then feed it to your test method.
✅ 77. What is @Parameters vs @DataProvider?
Answer:
| Feature | @Parameters | @DataProvider |
|---|---|---|
| Source | testng.xml | Java method |
| Data type | Only Strings | Any object type |
| Use case | One-time config | Multiple iterations |
Use @Parameters for external configuration, @DataProvider for data-driven testing.
✅ 78. Can we run TestNG tests without testng.xml?
Answer:
Yes. You can:
-
Run from IDE (Eclipse/IntelliJ) directly.
-
Run using annotations only.
-
Use Maven Surefire with class/method targeting.
However, testng.xml is required for:
-
Grouping.
-
Parallel testing.
-
Parametrization.
✅ 79. Can we pass parameters from testng.xml to @DataProvider?
Answer:
No. You cannot directly pass testng.xml parameters to @DataProvider. But you can pass them to the test method via @Parameters, and then combine with @DataProvider.
Alternative:
-
Read system/environment values inside
@DataProvider.
✅ 80. How to create a parallel test framework using TestNG?
Answer:
Steps:
-
Use
parallelattribute intestng.xml.
<suite name="ParallelTest" parallel="tests" thread-count="3">...</suite>
-
Pass browser/device using
@Parametersor@DataProvider. -
Write test methods that launch in separate threads.
-
Ensure thread-safety in WebDriver using ThreadLocal.
✅ 81. What is the use of @Listeners annotation in TestNG?
Answer:
@Listeners is used to attach a custom listener class to a test class. This helps capture events during test execution like success, failure, skip, etc.
@Listeners(MyTestListener.class)public class MyTestClass {@Testpublic void sampleTest() { ... }}
Where MyTestListener implements ITestListener or ISuiteListener.
Common use: capturing screenshots on failure, custom logging, reporting.
✅ 82. What interfaces are available in TestNG for listeners?
Answer:
TestNG provides multiple listener interfaces:
-
ITestListener– for individual test method events (start, success, failure, skip) -
ISuiteListener– for suite-level events (beforeSuite, afterSuite) -
IClassListener– before/after class execution -
IInvokedMethodListener– before/after every method (test/config) -
IReporter– for creating custom reports -
IAnnotationTransformer– to modify annotations at runtime
These are helpful for enhancing test execution tracking and reports.
✅ 83. What is IAnnotationTransformer in TestNG?
Answer:
IAnnotationTransformer is a listener interface that allows you to modify annotations like @Test, @BeforeMethod, etc. at runtime.
Use case: Enable/disable tests dynamically or set retry logic.
Example:
public class MyTransformer implements IAnnotationTransformer {public void transform(TestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {annotation.setEnabled(false); // disables all tests dynamically}}
Register using:
@Listeners(MyTransformer.class)
✅ 84. What is TestNG.xml file and why is it used?
Answer:
testng.xml is the configuration file for TestNG. It defines:
-
Test classes and packages
-
Groups and included/excluded tests
-
Parallel execution settings
-
Parameters
Example:
<suite name="Suite1"><test name="LoginTests"><classes><class name="com.tests.LoginTest" /></classes></test></suite>
It provides flexibility and centralized control over test execution.
✅ 85. How can we exclude specific test methods in testng.xml?
Answer:
Use the <methods> tag with <exclude>:
<class name="com.tests.LoginTest"><methods><exclude name="invalidLoginTest" /></methods></class>
This will skip the invalidLoginTest() during execution.
✅ 86. Can we pass parameters from testng.xml to test methods?
Answer:
Yes. Use @Parameters and define them in testng.xml.
In test class:
@Parameters({"username", "password"})@Testpublic void login(String user, String pass) { ... }
In XML:
<parameter name="username" value="admin"/><parameter name="password" value="admin123"/>
This is helpful for environment-specific testing.
✅ 87. What happens if a dependent test fails?
Answer:
If a test method depends on another (using dependsOnMethods or dependsOnGroups) and the parent test fails or is skipped, then the dependent test is also skipped.
Example:
@Testpublic void loginTest() {Assert.fail(); // fails}@Test(dependsOnMethods = "loginTest")public void dashboardTest() {// will be skipped}
✅ 88. How do you perform soft assertions in TestNG?
Answer:
Use SoftAssert class. Unlike hard assertions, soft assertions don’t stop execution after failure.
SoftAssert softAssert = new SoftAssert();softAssert.assertEquals(actual, expected);softAssert.assertTrue(condition);softAssert.assertAll(); // This will collect and throw all failures
Useful when you want to validate multiple checkpoints in a single test.
✅ 89. How do you retry failed tests in TestNG?
Answer:
-
Create a class that implements
IRetryAnalyzer.
public class RetryAnalyzer implements IRetryAnalyzer {private int retryCount = 0;private static final int maxRetry = 2;public boolean retry(ITestResult result) {if (retryCount < maxRetry) {retryCount++;return true;}return false;}}
-
Attach it to your test:
@Test(retryAnalyzer = RetryAnalyzer.class)public void flakyTest() {Assert.assertTrue(false);}
✅ 90. How to ignore a test method in TestNG?
Answer:
Use enabled = false in the @Test annotation.
@Test(enabled = false)public void testToSkip() {// this test will be ignored}
Alternatively, exclude it in testng.xml.
✅ 91. What is grouping in TestNG and why is it useful?
Answer:
Grouping allows you to organize test cases by category or functionality, and run selected groups.
Example:
@Test(groups = "smoke")public void test1() {}@Test(groups = "regression")public void test2() {}
Run only smoke tests via XML:
<groups><run><include name="smoke"/></run></groups>
Helps in selective, faster test execution.
✅ 92. What is alwaysRun = true in TestNG?
Answer:
This ensures the test runs even if its dependencies failed or skipped.
@Test(dependsOnMethods = "login", alwaysRun = true)public void logout() {// will run even if login fails}
Useful for cleanup, logout, or screenshot-taking tests.
✅ 93. Can we attach multiple listeners in TestNG?
Answer:
Yes. You can add multiple listeners using:
@Listeners({Listener1.class, Listener2.class})public class MyTest { ... }
Or define in testng.xml:
<listeners><listener class-name="com.listeners.Listener1"/><listener class-name="com.listeners.Listener2"/></listeners>
✅ 94. How can you set a test to run for a specific time only?
Answer:
Use timeOut attribute.
@Test(timeOut = 5000) // in millisecondspublic void longRunningTest() {// must complete in 5 seconds}
If the method takes longer, it will fail with timeout error.
✅ 95. What is the order of TestNG annotations?
Answer:
TestNG follows this execution order:
-
@BeforeSuite -
@BeforeTest -
@BeforeClass -
@BeforeMethod -
@Test -
@AfterMethod -
@AfterClass -
@AfterTest -
@AfterSuite
This helps in structuring test setup and teardown in an organized way.
✅ 96. Can we run tests in parallel at method level?
Answer:
Yes. In testng.xml, set:
<suite name="ParallelTest" parallel="methods" thread-count="3">
This runs test methods in parallel, which reduces execution time.
Be sure to manage thread-safety when using shared resources like WebDriver.
✅ 97. How to run failed tests again automatically in TestNG?
Answer:
-
After test run, TestNG generates a file:test-output/testng-failed.xml
-
You can re-run the failed tests using this XML:
java org.testng.TestNG test-output/testng-failed.xml
Also works with Maven or CI tools.
✅ 98. How to integrate TestNG with Maven?
Answer:
-
Add TestNG dependency in
pom.xml:
<dependency><groupId>org.testng</groupId><artifactId>testng</artifactId><version>7.8.0</version><scope>test</scope></dependency>
-
Use Surefire plugin to run tests:
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>3.0.0</version></plugin>
-
Run:
mvn test
✅ 99. How to generate HTML reports in TestNG?
Answer:
TestNG generates default HTML reports in the test-output folder.
You can also generate advanced reports using:
-
Extent Reports
-
Allure Reports
-
ReportNG
Example for Extent:
-
Add Extent dependency
-
Use
ExtentHtmlReporter -
Attach it to test events inside a listener
✅ 100. How to run multiple suites in TestNG?
Answer:
Create a suite-of-suites XML:
<suite name="MasterSuite"><suite-files><suite-file path="smoke.xml"/><suite-file path="regression.xml"/></suite-files></suite>
Then run the MasterSuite.xml. This helps manage multiple suites in a large project.