TestNG Interview Questions Answers For QA Engineers 2026
TestNG is a topic that almost every QA engineer is expected to be well-versed with during automation interviews. While many candidates understand the basics, interviews often go deeper, covering annotations, test execution flow, assertions, parallel testing, and real-world framework usage.
This article includes the most important TestNG interview questions and answers, explained in simple and practical language. It is designed to help QA engineers clearly explain concepts, understand how TestNG is used in automation projects, and prepare confidently for interviews in 2026.
So let’s get started!
Assertions in TestNG (Hard vs Soft)
1. What is the difference between hard and soft assertions?
Hard assertion stops the test as soon as it fails. If one check fails, the remaining steps are not executed. This is useful when the next steps depend on the current one. Soft assertion does not stop the test immediately and lets all steps run. It collects all failures and shows them together at the end.
2. When should you prefer soft assertions in UI tests?
Soft assertions are useful when you want to verify many things on the same screen. For example, checking page title, logo, buttons, labels, and messages together. Even if one check fails, the test continues and validates the rest. This helps you see all UI issues in one run.
3. How do you ensure soft assertion failures are reported at the end?
In TestNG, you must call assertAll() at the end of the test method. This tells TestNG to throw all collected failures at once. If you forget this step, the test will still pass even if some checks failed. So assertAll() is mandatory when using soft assertions.
4. What’s a good assertion strategy to reduce flaky failures?
Only assert things that are stable and important for the test. Avoid checking values that change often, like timestamps or random IDs. Always wait properly for elements before asserting. Keep assertions clear, limited, and meaningful so failures are easy to understand.
Attributes: Priority, Groups, Dependencies, Timeouts
1. What are dependsOnMethods and dependsOnGroups?
dependsOnMethods is used when one test should run only after another specific test passes. dependsOnGroups is used when a test depends on a whole group of tests. If the main test fails, all dependent tests are skipped. This is useful for flows like login - search - logout.
Learn via our Video Courses
2. What is priority in TestNG, and how does ordering work?
Priority decides the order in which test methods run. Lower priority value runs first, so priority 1 runs before priority 2. If no priority is given, TestNG treats it as priority 0. When two tests have the same priority, TestNG runs them in alphabetical order of method names.
3. What are TestNG groups and why are they useful?
Groups let you tag test cases into categories like smoke, regression, or sanity. This helps you run only selected tests instead of the full suite. Groups are useful when you want faster feedback, like running only smoke tests after a build. They also make large test suites easier to manage.
4. How do you include/exclude groups during execution?
You can include or exclude groups using the testng.xml file. Inside the <groups> tag, you mention which groups to include or exclude. This way, you don’t need to change code to control what runs. You just change the XML and rerun the suite.
5. What is the difference between dependency and priority?
Priority controls order but does not care if a test fails. Dependency controls flow based on pass or fail. Even if a high-priority test fails, the next priority test will still run. But if a test has a dependency and the main test fails, the dependent test will be skipped.
6. What are timeOut, invocationCount, and threadPoolSize used for?
timeOut sets a maximum time limit for a test to finish, or else it fails. invocationCount runs the same test multiple times. threadPoolSize is used with invocationCount to run those multiple runs in parallel. These are useful for performance and stability testing.
Build & CI Execution (Maven + Jenkins) for TestNG
1. How do you run TestNG tests using Maven (Surefire conceptually)?
Maven uses the Surefire plugin to run TestNG tests. Surefire looks for test classes and the testng.xml file. When you run a Maven command, it automatically triggers TestNG. This lets you run tests from terminal or CI without opening an IDE. It is the standard way to run automation in pipelines.
Read more at: Top Maven Interview Questions (2026)
2. How do you pass suite files / groups / parameters from the command line?
You can pass a suite file using the Maven command with a system property. Groups and parameters are also passed as system properties. TestNG reads these values during execution. This way, you don’t change code for different runs. It makes automation flexible for different needs.
3. How do you publish TestNG reports/artifacts in Jenkins?
After Jenkins runs tests, it stores the result files as build artifacts. You configure Jenkins to archive TestNG report folders. Jenkins can also show test results on the job page. This helps the team see pass and fail clearly. Reports can be downloaded anytime.
You can also checkout: Top 30+ Jenkins Interview Questions and Answers (2026)
4. How do you manage environment-specific configs in CI runs?
You keep separate config files for QA, UAT, and Prod. Jenkins passes the environment name as a parameter. Your framework loads the right config based on that value. This lets the same code run in different environments easily. No code change is needed for switching env.
You can read checkout more such questions at: CI/CD Interview Questions and Answers(2026)
Parallel Execution in TestNG
1. What is thread-count, and how do you choose a safe value?
thread-count decides how many tests can run at the same time. A higher value means faster execution but more load on the system. You should choose a value based on your machine power, memory, and browser limits. Start small and increase slowly to find a stable number.
2. How do you enable parallel execution in TestNG?
Parallel execution is enabled from the testng.xml file. You add the attribute parallel in the suite or test tag and set a value like methods, classes, or tests. You also add thread-count to control how many threads should run. Once this is set, TestNG runs multiple tests at the same time.
3. What’s the difference between parallel=methods vs classes vs tests?
parallel=methods runs test methods at the same time, even from the same class. parallel=classes runs different classes in parallel, but methods inside one class run one by one. parallel=tests runs different <test> blocks from testng.xml in parallel. The choice depends on how your framework is designed.
4. Why do Selenium tests fail in parallel, and how do you fix it (WebDriver isolation)?
Selenium tests often fail in parallel because they share the same WebDriver instance. When two tests use one driver, actions mix up. To fix this, each test should have its own WebDriver. You can use ThreadLocal to give every thread its own driver.
Parameterization & Data-Driven Testing in TestNG
1. What is @Parameters in TestNG, and where do values come from?
@Parameters is used to pass values into test methods from outside the code. The values usually come from the testng.xml file. This is helpful when you want to run the same test in different environments like QA, UAT, or Prod. You just change the XML, not the code.
2. How do you pass parameters via testng.xml?
In testng.xml, you define parameters using the <parameter> tag. Each parameter has a name and a value. In your test method, you use the same name inside @Parameters to receive that value. When the suite runs, TestNG automatically sends the value to your test.
3. What is @DataProvider and how is it different from @Parameters?
@DataProvider is used when you want to run the same test many times with different sets of data. It returns multiple values, usually in rows and columns. @Parameters normally sends only one set of values from XML. So, @Parameters is good for environment setup, and @DataProvider is better for testing many data combinations.
4. How do you feed data from CSV/JSON/DB into a DataProvider (approach)?
First, you read the data from CSV, JSON, or a database using normal Java code or libraries. Then you store that data in a two-dimensional array or iterator. That array is returned from the DataProvider method. TestNG uses that data to run the test again and again.
5. Do you run DataProvider tests in parallel safely?
You enable the parallel option in the DataProvider using parallel=true. Each test run should use its own data and its own objects. Avoid sharing variables between tests. Use ThreadLocal or separate browser instances so tests don’t clash with each other.
Selenium + TestNG Framework Questions
1. Why is TestNG commonly used with Selenium automation?
TestNG works very well with Selenium for managing test execution. It helps control the order, priority, and dependencies of tests. You can also run tests in parallel to save time. It gives good reports and easy configuration using XML. This makes large automation projects easier to handle.
You can read more questions at: Top 70+ Selenium Interview Questions and Answers (2025)
2. How do you structure a TestNG automation framework (base test, utils, page objects)?
You keep a common setup, like driver and confi,g in a BaseTest class. Page Objects contain all locators and page actions. The utils folder has reusable helpers like waits and screenshots. Test classes only contain test logic and validations. This structure keeps code clean and easy to maintain.
3. How do you do cross-browser execution using TestNG suites?
You define the browser name as a parameter in testng.xml. You create different tests for Chrome, Firefox, and other browsers. TestNG passes the browser value to your setup method. The driver is launched based on that value. This helps test your app on multiple browsers easily.
4. How do you manage the WebDriver lifecycle using TestNG annotations?
You create the driver in @BeforeClass or @BeforeMethod. You use it inside your test methods. You close or quit the driver in @AfterClass or @AfterMethod. This keeps tests clean and avoids memory issues. It also prevents browser sessions from piling up.
5. How do you implement tagging (smoke/regression) using groups?
You tag tests using groups in the @Test annotation. For example, @Test(groups="smoke") or @Test(groups="regression"). In testng.xml, you include or exclude these groups. This lets you run only the needed tests easily. It saves time during quick checks before releases.
TestNG Annotations Interview Questions
1. Explain @BeforeSuite, @BeforeTest, @BeforeClass, @BeforeMethod (and corresponding @After*)
- @BeforeSuite runs once before the entire test suite starts, usually for setup like database or report config.
- @BeforeTest runs before a <test> tag in testng.xml, often used for setting test-level data.
- @BeforeClass runs once before any method in a class, like opening a browser.
- @BeforeMethod runs before every test method, mostly for resetting test state.
- The @After* annotations run in reverse order after execution.
2. What is the execution order of TestNG annotations?
TestNG runs setup annotations from the outer level to the inner level. First comes @BeforeSuite, then @BeforeTest, then @BeforeClass, and then @BeforeMethod. After the test method runs, cleanup happens in reverse: @AfterMethod, @AfterClass, @AfterTest, and finally @AfterSuite. This order stays the same unless you change it using priorities or dependencies.
3. When do you use @BeforeMethod vs @BeforeClass?
Use @BeforeClass when the setup is needed only once for all tests in a class, like launching a browser. Use @BeforeMethod when the setup must happen before every test, like navigating to a page or resetting data. @BeforeMethod is useful when each test should start fresh. @BeforeClass is better when setup is heavy and should not repeat.
4. Can you run configuration methods conditionally? How?
Yes, configuration methods can be run conditionally. You can use attributes like enabled=false to skip them. You can also control execution using groups and group dependencies. Another way is using dependsOnMethods or dependsOnGroups, so setup runs only if certain tests pass.
5. What is @Test, and which attributes do you use most?
@Test marks a method as a test case. It tells TestNG that this method should be executed as part of the test run. Commonly used attributes are priority for execution order, groups for categorizing tests, dependsOnMethods for dependencies, and enables to skip a test. Sometimes, dataProvider is used to run the same test with multiple inputs.
You can find more such questions at: Top Software Testing Interview Questions (2025)
TestNG Listeners, Retry, and Custom Reporting
1. What are TestNG listeners, and why do teams use them?
Listeners are special classes that watch what is happening during test execution. They get triggered when a test starts, passes, fails, or is skipped. Teams use them to add extra actions like logging, taking screenshots, or updating reports. They help make automation more useful and informative.
2. Name common listeners (ITestListener, ISuiteListener) and their use cases.
ITestListener is used to track events of each test method, like start, pass, fail, and skip. It is mostly used for logging and taking screenshots on failure. ISuiteListener works at the suite level and runs before and after the entire suite. It is useful for setup like report creation and cleanup after all tests finish.
3. How do you capture screenshots on failure using listeners (Selenium + TestNG)?
You implement ITestListener in a class. In the onTestFailure method, you write code to take a screenshot using Selenium. Then you save the screenshot with the test name. This runs automatically whenever a test fails.
4. What is a RetryAnalyzer, and when should you avoid retries?
RetryAnalyzer is used to rerun failed tests automatically. It is helpful when failures happen due to network issues or temporary glitches. But you should avoid retries if the test is failing due to a real bug. Too many retries can hide real problems.
5. How do you generate and customize reports in TestNG?
TestNG gives default HTML and XML reports after execution. For better reports, teams use tools like Extent Reports or Allure. You connect these tools using listeners. Then you customize layout, colors, screenshots, and logs in the report.
TestNG Overview & Core Concepts Interview Questions
1. What are the main features of TestNG?
TestNG uses annotations like @test, @BeforeMethod, and @AfterClass to control when and how tests run. It supports grouping, so you can run only specific sets like smoke or regression tests. You can define dependencies between tests so one runs only if another passes. It also allows parameterization, which means running the same test with different data.
2. TestNG vs JUnit: key differences and when to choose which?
JUnit is mainly used for simple unit testing by developers. TestNG is more suitable for automation frameworks and large test suites. TestNG easily supports grouping, dependencies, and parallel execution. Use JUnit for basic code testing and TestNG for Selenium automation projects.
3. What is a TestNG suite, test, class, and method?
A suite is the top-level and is defined in the testng.xml file. A test is a section inside a suite that groups related test cases. A class is a Java file that contains test methods. A method is the actual test case written using the @Test annotation.
You can also check out: Automation Testing Interview Questions
4. What is TestNG and why is it used in automation frameworks?
TestNG is a testing framework mostly used with Selenium for automating test cases in Java. It helps manage large numbers of test cases in an organized way. You can control the order of execution, set priorities, and manage dependencies. It also supports parallel execution and detailed test reports, which makes automation easier.
testng.xml Suite Configuration Interview Questions
1. How do you organize suites for smoke/regression/sanity?
You usually create separate suites or separate tests inside one suite. For example, one suite for smoke, one for sanity, and one for regression. Each suite includes only the groups or classes related to that type of testing. This helps you run quick checks or full testing as needed. It also makes test planning more structured.
2. How do you run only specific classes/methods from testng.xml?
In testng.xml, you can mention exact class names under the <classes> tag. If you want only some methods, you use the <methods> tag inside a class. This way, only selected tests run without touching the code. It is useful when debugging or re-testing failed cases.
3. How do you exclude specific tests/methods from execution?
You can exclude tests using groups or method names in testng.xml. Under the <groups> tag, you can write which groups to exclude. Inside <methods>, you can also use <exclude> to skip specific methods. This is useful when some tests are broken or not needed. You can bring them back anytime by changing XML.
4. How do you configure groups in testng.xml?
In testng.xml, you use the <groups> section. Inside it, you write <include> or <exclude> with group names. This controls which group of tests should run. It helps you run only smoke, sanity, or regression tests easily. You don’t need to touch test code for this.
5. 1. What is testng.xml, and what problems does it solve?
testng.xml is a configuration file used by TestNG to control how tests run. It lets you choose which tests, classes, and methods to execute. You don’t need to change code again and again for different runs. You just change this file to manage execution easily. This saves time and avoids mistakes in code.