0% found this document useful (0 votes)
6 views12 pages

TestNG Interview Questions and Insights

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views12 pages

TestNG Interview Questions and Insights

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1. What is TestNG?

2. Mention some of the Advantages of TestNG?

3. Explain the difference between Test and Test Suite in TestNG.

4. Why should we use TestNG in Selenium Framework?

5. How to add TestNG maven dependency in [Link]?

6. What is [Link] File?

7. What is annotation in TestNG?

8. List some of the most common annotations used in TestNG.

9. What are PreCondition, Test, and PostCondition Annotations in TestNG?

10. What is parameterization in TestNG?

11. What are the Priorities in TestNG?

12. How to Define Grouping in TestNG?

13. How can we exclude a group in TestNG?

14. What are assertions in TestNG?

15. List down common Assertions used in TestNG.

16. Can you explain about handling dependent test Execution in TestNG?

17. How to handle Parallel execution in TestNG?

18. How to Define TimeOut in TestNG?

19. What is TestNG Listners?

20. Tell me about Data Providers in TestNG.

21. How Factory Different from DataProvides?

22. Name the Interface which can be used to retry failed tests?

23. What is the difference between TestNG and Junit?

24. What is expectedExceptions Attribute in TestNG?

25. How to disable a test in TestNG? Or How to skip tests in TestNG?

26. What is Inclusion & Exclusion Groups in TestNG?

27. What is the invocation count in TestNG?


1. What is TestNG?

TestNG is an automation testing framework. TestNG is designed based on JUnit however


many disadvantages of JUnit are addressed which makes TestNG unique and easy for an end
to end Testing.

2. Mention some of the Advantages of TestNG?

 Easy to group the test cases

 Easy to Configure multi-browser testing

 Easy to integrate with other frameworks such as Maven, Jenkins

 Comes with built-in HTML reports

 Allows to prioritize test cases

 Provides the option to parameterize test cases

 Provides the option to perform Data-driven testing

 Easy to implement assertions

3. Explain the difference between Test and Test Suite in TestNG.

 TestNG Test is a single test file or test case.

 TestNG Test Suite is a collection of tests that can be configured in TestNG using .xml
file.

 A suite is represented by one XML file. It can contain one or more tests and is
defined by the <suite> tag.

 A test is represented by <test> and can contain one or more TestNG classes.

 A TestNG class is a Java class that contains at least one TestNG annotation. It is
represented by the <class> tag and can contain one or more test methods.

 A test method is a Java method annotated by @Test in your source.

 [Link] example

< <suite name = "Suite Name">

<test name = "Test Name">

<classes>

<class name = "Test Class Name" />

</classes>
</test>

</suite>

4. Why should we use TestNG in Selenium Framework?

Selenium provides API to simulate browser actions, it doesn’t provide assertions, reporters,
and execution configuration. Using Selenium with TestNG makes complete Automation
Framework.

5. How to add TestNG maven dependency in [Link]?

Open [Link] in your maven project, you need to simply add the below entries to
[Link] file.

<dependency>

<groupId>[Link]</groupId>

<artifactId>testng</artifactId>

<version>6.8</version>

<scope>test</scope>

</dependency>

6. What is [Link] File?

 [Link] file helps us to configure our test execution behaviour. Such as.

 Running Multiple Tests

 Include and Exclude Test cases

 Adding Dependancies

 Parameterization of Test cases

 Parallel Execution

7. What is annotation in TestNG?

The TestNG annotations are used to control the flow of execution in test script. TestNG
annotations are used in combination with methods or classes.

8. List some of the most common annotations used in TestNG.

 Annotations  Description

 @BeforeSuite  The @BeforeSuite method in TestNG runs before the


execution of all other test methods.

 @AfterSuite  The @AfterSuite method in TestNG runs after the execution


of all other test methods.

 @BeforeTest  The @BeforeTest method in TestNG runs before the


execution of all the test methods that are inside that folder.

 @AfterTest  The @AfterTest method in TestNG executes after the


execution of all the test methods that are inside that folder.

 @BeforeClass  The @BeforeClass method in TestNG will run before the first
method invokes the current class.

 @AfterClass  The @AfterClass method in TestNG will execute after all the
test methods of the current class execute.

 @BeforeMethod  The @BeforeMethod method in TestNG will execute before


each test method.

 @AfterMethod  The @AfterMethod method in TestNG will run after each


test method is executed.

 @BeforeGroups  The @BeforeGroups method in TestNG runs before the test


cases of that group are executed. It executes just once.

 @AfterGroups  The @AfterGroups method in TestNG runs after the test


cases of that group are executed. It executes only once.

9. What are PreCondition, Test, and PostCondition Annotations in TestNG?

 Precondition annotations are those annotations of TestNG that are executed before
the execution of test methods The Precondition annotations are @BeforeSuite,
@BeforeClass, @BeforeTest, @BeforeMethod

 Test annotation Methods annotated with @Test are called test methods which serve
as a unit test

 Postcondition annotations are those annotations that are executed after the
execution of all the test methods. The postcondition annotation can be @AfterSuite,
@AfterClass, @AfterTest, @AfterMethod.

10. What is parameterization in TestNG?

 Parameterized tests allow the user to run the same test with multiple times but using
different values.

 Example:

 Test Script
public class ParameterizedTest {

@Test

@Parameters("fruit")

public void parameterTest(String fruit) {

[Link]("Parameterized value is : " + fruit);

 [Link] file

<suite name = "MySuite">

<test name = "Mytest">

<parameter name = "fruit" value="apple"/>

<classes>

<class name = "ParameterizedTest" />

</classes>

</test>

</suite>

11. What are the Priorities in TestNG?

Prioritization in TestNG provides execution sequence to the methods so that they can run on
order. One after the other in the specified format.

@Test (priority = 0)

public void myMethod1(){

//test code

@Test (priority = 10)

public void myMethod2(){

//test code

12. How to Define Grouping in TestNG?


 Grouping in TestNG lets you group multiple test cases in named groups. Once you
define the group you can run single or multiple groups.

 Example

 Test Script

public class Test

@Test(groups = { "group1", "group2" })

public void method1()

//Code

@Test(groups = {"group2"} )

public void method2()

//Code

@Test(groups = {"group1"})

public void method3()

//Code

 [Link] file

<suite name="TestNG Grouping">

<test verbose="2" preserve-order="true">

<groups>
<run>

<include name = "group1"></include>

<include name = "group2"></include>

</run>

</groups>

<classes>

<class name="[Link]"></class>

</classes>

</test>

</suite>

13. How can we exclude a group in TestNG?

 TestNG allows us to ignore tests. This can be used using [Link] file.

 If we want to exclude the test group with name “Example” we can do it like below.

<suite name="Test Suite" >

<test name="Test" >

<groups>

<run>

<exclude name = "Example">

</exclude>

</run>

</groups>

<classes>

<class name="TestClass" />

</classes>

</test>

</suite>

14. What are assertions in TestNG?


 TestNG Assertions help to verify the expected result and the actual result. By
verifying the result we can pass/fail the tests.

 Syntax:

 [Link](actual, expected, message)

 There are two types of Asserts in TestNG

 Hard Assert in TestNG throws an AssertException, when an assert statement fails and
the test suite continues with the next @Test method.

 Soft Assert in TestNG does not throw an exception when an assert fails and would
continue with the next step after the assert statement. However, we can collect the
error logs and which can be verified later

15. List down common Assertions used in TestNG.

 assertEqual(actual,expected,message)

 assertEquals(actual,expected,message)

 assertNotEquals(actual, expected, message);

 assertTrue(condition)

 assertTrue(condition, message)

 assertFalse(condition)

 assertFalse(condition, message)

 assertNull(object);

 assertNotNull(object);

 assertNotSame(Object actual, Object expected)

 assertSame(Object actual, Object expected)

16. Can you explain about handling dependent test Execution in TestNG?

 A single or group of tests depends on one or more tests are called dependent tests.

 For example, test1 is depending test2. These scenarios can be handled in TestNg
using dependsOnMethods or dependsOnGroups attributes are used for handling
dependencies of tests in TestNG.

public class DependencyTest {

@Test (dependsOnMethods = { "Test2" })

public void Test1() {


[Link]("Executes After Test2");

@Test

public void Test2() {

[Link]("Executes First");

Note: A similar way dependsOnGroups can be used to define the dependency for groups.

17. How to handle Parallel execution in TestNG?

 Parallel Testing in TestNG is a technique in which multiple tests are executed


simultaneously in different threads to reduce execution time.

 The [Link] parallel attribute is used for this purpose.

 TestNG allows us to execute tests parallelly at various levels.

 Method: Runs the parallel tests on all @Test methods in TestNG

 Ex: <suite name = "Parallel Test" parallel = "methods">

 Test: All the test cases present inside the <test> tag will run with this value.

 Ex: <suite name="Test class Suite" parallel="tests" thread-count="2">

 Classes: Each class will be started and executed simultaneously in different threads

 Ex: <suite name="Parallel" parallel="classes" thread-count="2">

 Instances: TestNG will run all the methods in the same instance in the same thread,
but two methods on two different instances will be running in different threads

 Ex: <suite name = “Parallel Test Suite” parallel = “instances” thread-count = “4”>

18. How to Define TimeOut in TestNG?

 TestNG allows users to configure a time for a test to completely execute.

 Timeout attribute with @Test tag is used for this purpose.

 Example:

@Test ( timeOut = 500 )

Public void timeOutExample(){

//code
}

 The timeout can also be handled using the [Link] file.

 Example:

 <suite name= "Test Suite" time-out="500">

 <test name="Test1" verbose="2" time-out="300">

19. What is TestNG Listners?

 TestNG Listeners are TestNG annotations that “listen” to the events in a script. This
helps to modify the default behavior of TestNG. If we want to take a screenshot of
test failures we need to Listen to Test Failure. @Listeners annotations are used at the
class level to implement listeners.

 The ITestListener is the most used TestNG interface it has a method like onTestStart,
onTestSuccess, onTestFailure, onTestSkipped, etc. using this we can configure
reporters and custom logging capability in TestNG.

20. Tell me about Data Providers in TestNG.

 TestNG Data Provider is a method of a class which is having annotation


@DataProvider and returns an array of objects, which can be used inside our actual
test.

 Example:

public class DataProviderClass {

@Test (dataProvider="getData")

public void loginTest(String FirstName, String LastName){

[Link]("First Name is "+ FirstName);

[Link]("Last Name is "+ LastName);

@DataProvider(name="getData")

public Object[][] getData(){

Object [][] data = new Object [2][2];

data [0][0] = "FirstName1";


data [0][1] = "LastName1";

data[1][0] = "FirstName2";

data[1][1] = "LastName2";

return data;

21. How Factory Different from DataProvides?

 @DataProvider – A test method that uses @DataProvider will be executed multiple


times based on the configuration. The test method will be executed using the same
instance of the test class to which the test method belongs.

 @Factory – A factory will execute all the test methods present inside a test class
using separate instances of the class.

22. Name the Interface which can be used to retry failed tests?

IRetryAnalyzer is used to retry failed tests.

Steps to Create Retry Failed Tests

 Create a class to implement IRetryAnalyzer

 Create another class ‘RetryListenerClass’ by Implementing the‘


IAnnotationTransaformer’ interface, which will use the implementation of the above
class

 Add Listener to [Link] File

23. What is the difference between TestNG and Junit?

 Junit is mostly focused on Unit Testing, TestNG has rich features for both Unit and
end to testing.

 Generating HTML Reports is a complicated task in JUnit, TestNG provides an easy way
to generate HTML reports.

 Advanced Features such as Dependency Test, Parameterization, Priority setting is


sometimes a complex task or we need to follow a workaround to implement these
features, TestNG comes with the default set of features to support mentioned
features.

24. What is expectedExceptions Attribute in TestNG?

If the exception thrown by the test is part of the the expectedExceptions attribute then the
exceptions are ignored and not marked as failure.

@Test(expectedExceptions = { [Link] })

public void exceptionTestOne() throws Exception {

throw new IOException();

25. How to disable a test in TestNG? Or How to skip tests in TestNG?

We can disable the test using enabled attribute. Setting enabled =false disables the test.

@Test(enabled = false)

public void Test() {

[Link]("Hello World");

26. What is Inclusion & Exclusion Groups in TestNG?

A Test Group which is included for test execution is called the Inclusion Group. A Test Group
which is excluded from test execution is called Exclusion Group

27. What is the invocation count in TestNG?

invocation count is an attribute, when used it executes the test a specified number of times.

public class MultipletimesRun {

@Test(invocationCount=5)

public void run five times(){

[Link]("Execute 5 times");

Common questions

Powered by AI

Parallel execution in TestNG is achieved using the 'parallel' attribute in testng.xml, which allows methods, tests, classes, or instances to run concurrently in different threads. By setting attributes such as parallel="methods", parallel="tests", parallel="classes", or parallel="instances", and specifying a thread-count, you can fine-tune the parallel execution scope and control the number of concurrent threads . This reduces execution time and optimizes resource usage in testing frameworks .

The TestNG XML file plays a crucial role in managing test execution behavior. It allows for the configuration of running multiple tests, including and excluding test cases, adding dependencies, parameterizing test cases, and executing tests in parallel . This flexible configuration capability enables testers to define complex test execution scenarios succinctly and manage them effectively .

Annotations in TestNG are used to control the flow of execution in test scripts. By using annotations such as @BeforeSuite, @AfterSuite, @BeforeTest, @AfterTest, @BeforeClass, and others, testers can set up and tear down test environments, manage preconditions and postconditions, and define the sequence of test execution. These annotations ensure that certain actions are performed at specific points during or around the test execution .

TestNG provides the IRetryAnalyzer interface to retry failed tests. To implement this, create a class that implements IRetryAnalyzer with logic to determine retry conditions, and another class, RetryListenerClass, implementing the IAnnotationTransformer interface to use the retry logic. This setup allows failed tests to be retried automatically based on the specified rules, helping improve test suite resilience by accounting for transient failures .

TestNG supports data-driven testing via the @DataProvider annotation, which allows a test method to run multiple times with different data sets. The @DataProvider method returns an array of objects that the test method listed under @Test(dataProvider="name") consumes. This allows for flexible, parameterized testing and is often used to run the same test with different variables ensuring broader coverage .

The expectedExceptions attribute in TestNG is used to specify that a test method must throw a particular exception to pass. If the specified exception is thrown, it is considered part of the expected outcome, and the test does not fail due to the exception. This is useful for scenarios where failures or exceptions are anticipated parts of the test logic, allowing testers to verify correct exception handling without marking the test as failed erroneously .

TestNG Listeners are interfaces that allow you to listen to events in a test script, such as test start, success, failure, and skipping. By implementing ITestListener and overriding methods such as onTestStart, onTestSuccess, and onTestFailure, users can customize logging, taking screenshots on failure, or implementing custom reporting mechanisms. Listeners provide a powerful mechanism to extend and modify default TestNG behavior and integrate custom functionality into the testing process .

The @DataProvider in TestNG allows a single test method to run multiple times with different data sets within the same instance of the test class, facilitating data-driven testing. In contrast, the @Factory annotation is used to execute all test methods in a test class using different instances of the class. While @DataProvider emphasizes running a method with varying data, @Factory focuses on creating multiple instances of a test class, each potentially configured differently, impacting test execution by increasing instance-level isolation .

TestNG offers several advantages over JUnit, including easier generation of HTML reports, built-in support for advanced features such as dependency tests, parameterization, and priority setting without requiring complex workarounds. These features make TestNG more suitable for both unit and end-to-end testing, whereas JUnit is primarily focused on unit testing .

Handling dependent test execution in TestNG is achieved using attributes like dependsOnMethods or dependsOnGroups. These attributes define dependencies between methods or groups, ensuring that a method runs only after the methods it depends on have successfully completed. This approach helps in maintaining the correctness and reliability of tests, especially when certain tests' outputs are prerequisites for others' inputs. If dependent tests fail, it prevents the subsequent execution, thereby indicating potential failures due to earlier steps .

You might also like