Testing Interviews Question and Answer
Testing Interviews Question and Answer
Basic Concepts
Q: What is TestNG?
A: TestNG is a testing framework inspired by JUnit and NUnit but introduces new functionalities
to make testing easier and more powerful.
----------------------------------------------------------------------------------------------------------------
A: Go to Eclipse -> Help -> Eclipse Marketplace -> Search for TestNG -> Install.
----------------------------------------------------------------------------------------------------------------
A: Better annotations, support for parallel testing, flexible test configuration, and better
reporting.
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
Annotations
----------------------------------------------------------------------------------------------------------------
A: @BeforeMethod runs before each test method, and @AfterMethod runs after each test
method.
100 TESTNG QUESTIONS AND ANSWERS
A:
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
Configuration
A: Create a testng.xml file with suite and test tags, then run it as a TestNG suite in Eclipse.
----------------------------------------------------------------------------------------------------------------
A: It includes <suite> and <test> tags, where <suite> can contain multiple <test> tags, and each
<test> can contain multiple <classes> and <methods> tags.
100 TESTNG QUESTIONS AND ANSWERS
A: Use the include and exclude tags within the groups tag in the testng.xml file.
----------------------------------------------------------------------------------------------------------------
A: The priority attribute is used to define the order in which the test methods should be
executed.
----------------------------------------------------------------------------------------------------------------
A: Configure the parallel attribute in the testng.xml file with values like tests, classes, or
methods.
----------------------------------------------------------------------------------------------------------------
Data Providers
A: Define a method annotated with @DataProvider that returns Object[ ][ ], then use
dataProvider attribute in @Test annotation.
----------------------------------------------------------------------------------------------------------------
A: Yes, by specifying the class and method name in the dataProviderClass attribute of the @Test
annotation.
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
A: Use the same @DataProvider method in multiple @Test methods by specifying the
dataProvider attribute.
----------------------------------------------------------------------------------------------------------------
Assertions
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
A:
assertEquals: Verifies that two values are equal, throwing an AssertionError if they are
not.
assertTrue: Verifies that a condition is true, throwing an AssertionError if it is not.
assertFalse: Verifies that a condition is false, throwing an AssertionError if it is not.
assertNull: Verifies that an object is null, throwing an AssertionError if it is not.
assertNotNull: Verifies that an object is not null, throwing an AssertionError if it is null.
assertSame: Verifies that two references point to the same object, throwing an
AssertionError if they do not.
assertNotSame: Verifies that two references do not point to the same object, throwing
an AssertionError if they do.
----------------------------------------------------------------------------------------------------------------
A: Use the soft Assert class which allows you to collect all errors and report them at the end.
----------------------------------------------------------------------------------------------------------------
A: Assert stops test execution if the condition fails, while verify (using soft assertions) collects
failures and continues execution.
100 TESTNG QUESTIONS AND ANSWERS
A: Create an instance of SoftAssert, call assert methods on it, and finally call assertAll() to report
all collected errors.
----------------------------------------------------------------------------------------------------------------
A: Listeners are interfaces that allow you to modify the behavior of TestNG and listen to events
like test start, finish, pass, fail, etc.
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
TestNG Suite
A: Create a testng.xml file with <suite> and <test> tags and specify the classes and methods to
be included.
100 TESTNG QUESTIONS AND ANSWERS
A: <suite> defines a group of tests, and <test> defines a single test which can include multiple
classes and methods.
----------------------------------------------------------------------------------------------------------------
A: Use the include and exclude tags within the <methods> tag in the testng.xml file.
----------------------------------------------------------------------------------------------------------------
A: No, a single testng.xml file can define only one <suite> tag.
----------------------------------------------------------------------------------------------------------------
A: Use the <parameter> tag within the <suite> or <test> tags in the testng.xml file.
----------------------------------------------------------------------------------------------------------------
Dependency Testing
A: Dependency testing allows you to specify dependencies between test methods, so a test
method can be skipped if its dependent method fails.
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
TestNG Parameters
A: Use the @Parameters annotation and define parameters in the testng.xml file.
----------------------------------------------------------------------------------------------------------------
A: Use the @Optional annotation to specify a default value for the parameter.
----------------------------------------------------------------------------------------------------------------
A: Define parameters in the testng.xml file and retrieve them using the @Parameters annotation
in the method that calls the @DataProvider.
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
A: Define multiple <parameter> tags in the testng.xml file and list the parameters in the
@Parameters annotation.
----------------------------------------------------------------------------------------------------------------
Parallel Testing
A: Parallel testing allows you to run multiple tests simultaneously to reduce execution time.
100 TESTNG QUESTIONS AND ANSWERS
A: Set the parallel attribute in the testng.xml file to tests, classes, or methods.
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
Advanced Topics
A: @Factory is used to create instances of test classes dynamically, allowing for different test
data setups.
----------------------------------------------------------------------------------------------------------------
A:
----------------------------------------------------------------------------------------------------------------
A: @Parameters is used to pass parameter values to test methods through the testng.xml file.
----------------------------------------------------------------------------------------------------------------
A: @Optional is used to specify a default value for a parameter in case it is not provided in the
testng.xml file.
100 TESTNG QUESTIONS AND ANSWERS
A: Create a new annotation by defining it with the @interface keyword and then use it in
combination with a custom implementation of IAnnotationTransformer to modify test behavior.
----------------------------------------------------------------------------------------------------------------
A: @BeforeSuite is used to define a method that should run before all tests in the suite are
executed.
----------------------------------------------------------------------------------------------------------------
A: @AfterSuite is used to define a method that should run after all tests in the suite have
completed.
----------------------------------------------------------------------------------------------------------------
A: Use the <include> and <exclude> tags within the <methods> tag to specify which tests to
include or exclude.
----------------------------------------------------------------------------------------------------------------
A: The <listeners> tag is used to specify listener classes that should be applied to the test suite.
----------------------------------------------------------------------------------------------------------------
A: Use the <groups> tag to define and configure groups, and specify included or excluded groups
within <run> tag.
----------------------------------------------------------------------------------------------------------------
A: The <parameter> tag is used to pass parameters to test methods defined in the @Parameters
annotation.
100 TESTNG QUESTIONS AND ANSWERS
A: Use the time-out attribute in the <suite> or <test> tag to specify a maximum time for test
execution.
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
A: assertSame verifies that two object references point to the same object.
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
A: ITestContext provides information about the test run and allows interaction with the test
context.
----------------------------------------------------------------------------------------------------------------
A: Implement the ITestListener interface and override its methods to perform actions on test
events.
100 TESTNG QUESTIONS AND ANSWERS
A: IAnnotationTransformer allows you to modify test annotations at runtime before they are
executed.
----------------------------------------------------------------------------------------------------------------
A: TestNG generates HTML reports automatically in the test-output folder after test execution.
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
Parallel Execution
A: Set parallel="methods" in the <suite> tag of the testng.xml file and specify thread-count.
----------------------------------------------------------------------------------------------------------------
A: Set parallel="tests" in the <suite> tag of the testng.xml file and specify thread-count.
----------------------------------------------------------------------------------------------------------------
A: Parallel execution may lead to data inconsistency if tests share the same data. Use thread-safe
techniques to manage data.
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
A: Set parallel="classes" in the <suite> tag of the testng.xml file and specify thread-count.
100 TESTNG QUESTIONS AND ANSWERS
Parameterization
A: Define parameters in testng.xml using <parameter> tags and retrieve them in the test method
using @Parameters annotation.
----------------------------------------------------------------------------------------------------------------
A: @Optional provides a default value for a parameter if it is not specified in the testng.xml file.
----------------------------------------------------------------------------------------------------------------
A: Yes, @Parameters can be used with @BeforeClass to pass parameters before any test
methods are executed.
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
A: Define multiple <parameter> tags in the testng.xml file and list the parameter names in the
@Parameters annotation.
----------------------------------------------------------------------------------------------------------------
TestNG Integration
A: Add the TestNG dependency to the pom.xml file and configure the surefire plugin to run
TestNG tests.
----------------------------------------------------------------------------------------------------------------
A: Use the TestNG plugin in Jenkins to run TestNG test suites and generate reports.
100 TESTNG QUESTIONS AND ANSWERS
A: Add TestNG tasks to the build.xml file and configure the Ant script to run TestNG tests.
----------------------------------------------------------------------------------------------------------------
A: Use TestNG to manage and execute Selenium WebDriver test scripts, allowing for better test
organization and reporting.
----------------------------------------------------------------------------------------------------------------
A: Implement the IReporter interface to create custom HTML or XML reports based on test
results.
----------------------------------------------------------------------------------------------------------------
TestNG Groups
A: A test group is a way to categorize test methods so they can be included or excluded as a
group during execution.
----------------------------------------------------------------------------------------------------------------
A: Use the groups attribute in the @Test annotation to assign methods to a group.
----------------------------------------------------------------------------------------------------------------
A: Use the <groups> tag with <run> and <include>/<exclude> tags to specify which groups to
run or skip.
----------------------------------------------------------------------------------------------------------------
A: Yes, list multiple group names in the groups attribute of the @Test annotation separated by
commas.
100 TESTNG QUESTIONS AND ANSWERS
A: Use the dependsOnGroups attribute in the @Test annotation to specify group dependencies.
----------------------------------------------------------------------------------------------------------------
Exception Testing
A: Use the expectedExceptions attribute in the @Test annotation to specify the exceptions that a
test method is expected to throw.
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
A: No, expectedExceptions attribute only checks for the exception type, not the message.
----------------------------------------------------------------------------------------------------------------
A: Use a try-catch block within the test method and assert the exception message using
assertion methods.
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
Miscellaneous
A: Use the SkipException class to throw a SkipException and skip the test dynamically.
100 TESTNG QUESTIONS AND ANSWERS
A: Implement the IRetryAnalyzer interface and override the retry method, then associate it with
@Test using the retryAnalyzer attribute.
----------------------------------------------------------------------------------------------------------------