TestNG, where NG stands for "next generation" is the next step towards writing test cases. It was developed by Cedric Beust to overcome the deficiency in JUnit by introducing some more functionalities and features.

Here, we have listed topic wise the best collection of frequently asked questions and the best possible answer for the interview.


Topic-wise TestNG Interview Questions with Answers for Freshers



TestNG interview questions

Frequently Asked Questions on TestNG

1. What is TestNG?
🅰 TestNG is an open-source automation testing framework based on JUnit by introducing some more functionalities and features. So, TestNG is more powerful than JUnit framework. It can be integrated with Selenium or any other automation tool to provide multiple features like assertions, reporting, parallel test execution, etc.

2. What are the important features of TestNG?
🅰 TestNG include the following features that are as follows:
1. TestNG uses more object-oriented and java features.
2. TestNG supports multiple types of Before/After annotations to create test cases.
3. It provides different types of assertions that helps in checking actual and expected results.

4. It provides priority and grouping features by which test cases can be grouped and prioritized easily.
5. TestNG internally generates HTML test report in a readable format. 
6. It supports parallel testing, integrated testing, load testing, etc.
7. TestNG allows Data-driven testing using @DataProvider annotations.
8. It supports for parameterizing test cases using @Parameters annotation.

3. What are some advantages of TestNG over JUnit?
🅰 TestNG have the following advantages over JUnit. They are as follows:
1. TestNG generates HTML test report in a readable format.
2. Test methods can be grouped and prioritized more easily that is not possible in the case of JUnit.
3. Annotations of TestNG are easy to understand for creating test cases.

4. In TestNG, we can execute multiple test cases in parallel to reduce execution time.
5. Using TestNG, we can define the dependency of one test method over another.
6. TestNG doesn't need to extend any class.
7. TestNG supports three additional annotations @Before/After Suite, @Before/After Test and @Before/After Group.

4. Why do we need TestNG in Selenium?
🅰 There are following reasons for integrating TestNG with Selenium.
1. Since Selenium WebDriver does not generate test report, by integrating TestNg with Selenium, we can easily generate test reports/results in proper readable format to know about how many test cases are passed, failed, and skipped.

2. We can execute selective test cases in TestNG.
3. Using TestNG, we can also execute failed test cases separately.
4. TestNG framework can be integrated easily with other tools like Maven, Jenkins, etc.
5. We can easily organize test cases in a proper way.

5. What are the basic steps required in writing TestNG test?
🅰 There are three basic steps required in writing TestNG test that is as follows:
1. Write down the business logic of the test and annotate it by TestNG annotations.
2. Create testing.xml file and add the information about your test
3. Run TestNG.

6. How to run test script in TestNG?
🅰 We can run test script in TestNG by right clicking on the TestNG class, click on "Run As" option and then select "TestNG test".

For more details go to link: https://www.scientecheasy.com/2019/05/how-write-first-testng-test-case.html

Frequently Asked Questions on TestNG Annotations

1. What are Annotations in TestNG?
🅰 Annotations in TestNG are a set of code that controls how method below them have to be executed i.e, the order of execution of methods below them will be decided by annotations that we give.

2. What are the different annotations available in the TestNG?
🅰 TestNG supports a powerful set of different annotations that run tests in an organized and predictable manner.

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

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

c. Postcondition annotations
The 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.

3. What is the sequence of execution of all TestNG annotations? 
🅰 The sequence of execution of all annotations in TestNG is as follows:
     👉 @BeforeSuite
     👉 @BeforeTest
     👉 @BeforeClass
     👉 @BeforeMethod
     👉 @Test
     👉 @AfterSuite
     👉 @AfterTest
     👉 @AfterClass
     👉 @AfterMethod

4. What is difference between Suite, Test, and Class?
🅰 Suite: A suite is made of one or more tests.
       Test: A test is made of one or more classes.
       Class: A class is made of one or more methods.

5. What is the difference between @BeforeClass and @BeforeMethod?
🅰 There are two main differences between @BeforeClass and @BeforeMethod. They are as follows:
1. The method with @BeforeClass will be executed only once before any of the tests in the current class are run whereas, a method annotated with @BeforeMethod will be executed before each method annotated with @Test.

2. @BeforeClass annotation can be used to set up the configuration and initialization which is common to all test methods in the current class. For example, we can set up driver configuration which will be common for all tests in the class. 

@BeforeMethod can be used to set that data which is repeating before each @Test annotated method.

6. What is the test method?
🅰 A method annotated with @Test is called test method which serves as a unit test. In the @Test method, we will write the logic of the application which we want to automate.

7. What is the return type of @DataProvider annotation provided by TestNG?
🅰 @DataProvider annotation return an object double array (Object [ ][ ]) as data to the test method.

8. What is the return type of @Factory annotation?
🅰  This annotation returns an array of class objects (Object [ ]).

For more details go to link: https://www.scientecheasy.com/2019/04/testng-annotations.html

Frequently Asked Questions on TestNG Priority and Groups

1. What is Priority in TestNG?
🅰 Priority is an attribute that tells TestNG which orders have to follow to run test method. It is used to set the order of test cases as ours need.

2. What is the default value for TestNG Priority?
🅰 The default value for TestNG priority is zero.

3. How to set Priority of test cases in TestNG?
🅰 TestNG priority attribute is useful to execute the test cases in order. It has the following general form to set the priority of test cases. 
Syntax:
     @Test(prioirty = 1) // Valid syntax.
     @Test(PRIORITY = 1) // Invalid sysntax. IDE will show it as a compilation error.

4. How will you execute methods or test cases in TestNG in different order/your order?
🅰 When we have multiple methods or test cases and want to execute in order, TestNG priority attribute is useful to execute multiple test cases in your order. If you have not mentioned test priority, TestNG will assign all @Test a priority as zero(0) and execute them.

5. How will you control multiple test cases?
🅰 Refer to Answer 4.

6. Define TestNG Groups.
🅰 Groups is an attribute in TestNG that allows us to perform groupings of test methods or test cases. It allows us to declare a set of test methods in a particular named group or multiple groups and run multiple groups of tests at different times or in different places.

For example, if we have 100 test cases of University and 10 test cases of College, and if you want to run all the test cases of University together in a single suite, this can be possible only through the grouping of test cases.

7. How to group multiple test methods in a single group using TestNG?
🅰 We can group multiple test methods by using groups parameter in the @Test annotation. The syntax for grouping multiple test methods in a single group is as follows:
Syntax: 
       @Test(groups = {"GroupName"})
For example:
      @Test(groups = {"Chemistry"})
           public void atom() {
                  ............
           }
      @Test(groups = {"Chemistry"})
            public void electorn()  {
                  ................
           }
Both test methods will execute in one group named Chemistry.

8. How to group multiple test methods in multiple groups?
🅰 The grouping of test methods belonging to multiple groups can be done by providing the group names as an array in the groups attribute of the @Test annotation. The below syntax lets you add test methods in multiple groups.
Syntax: 
      @Test(groups = { "GroupName1", "GroupName2", "GroupName3" .... })

9. How to group multiple test methods with Priority?
🅰 You can also group multiple test methods with priority. The syntax for grouping test methods with priority is as follows:
Syntax:
     @Test(groups={"GroupName"}, priority=0) // The test method annotated with this group will execute first.
     @Test(groups={"GroupName"}, priority=1) // The test method annotated with this group will execute after executing the first group.

10. What is Inclusion & Exclusion Groups in TestNG?
🅰 A group which is included in test execution is called inclusion group. A group which is excluded from test execution is called exclusion group.

11. How to disable a test in TestNG?
🅰 We can also disable tests on an individual basis by using the "enabled" property available on both @Test and @Before/After annotations. The syntax can be like this:
Syntax:  
      @Test(enabled = false)
      @Test(groups = {"Cricket Player"}, enabled=false)

12. What is Default Group, Partial Groups, and MetaGroups in TestNG?
🅰 Default Group: When an entire class is added to a group, it is called default group. It is a good way of defining a default group for all unit tests within a class.

Partial Groups: When you define groups at the class level and then add groups at the method level, it is called partial groups.

MetaGroups: When groups include other groups, these groups are called metagroups.

For more details go to link: https://www.scientecheasy.com/2019/04/testng-annotations.html

Frequently Asked Questions on TestNG @Test Annotation and its Supported Attributes

1. What is @Test Annotation in TestNG?
🅰 @Test annotation is a basic annotation in TestNG that marks a method as a test method. The test method annotated with @Test annotation tells TestNG that this method is a "test" and it should be executed when user runs the class.

2. What are the attributes supported by @Test annotation in TestNG?
🅰 @Test annotation supports lots of attributes that we can use with this annotation. Some of the important attributes supported by Test annotation are alwaysRun, dataProvider, dataProviderClass, dependsOnGroups, dependsOnMethods, enabled, priority, timeOut, etc.

3. Which attribute is used to run test method always?
🅰 The attribute "alwaysRun" supported by @Test annotation is used to run test methods always. It takes value as true or false. If we set true, this method will always execute even its depending method fails. It has the following general form.
Syntax:
        @Test(alwaysRun = true)

4. Which attribute is used to provide data to test method in Data-driven testing?
🅰 The dataProvider attribute is used to provide data to the test method directly in data-driven testing.
Syntax:
       @Test(dataProvider = "getData")

5. What is dependency in TestNG?
🅰 When we want to execute multiple test cases in a specific order, then we use the concept of dependency in TestNG.
There are two types of dependency attributes used in TestNG. They are
a. dependsOnMethods: The attribute dependsOnMethods is used to make the test method depend on a particular method. The test method annotated with @Test and attribute dependsOnMethods will run after executing all those methods on which this test method is dependent.
Syntax:
         @Test(dependsOnMethods={"Method1", "Method2". . .})

b. dependsOnGroups: This attribute is used to make test methods depend on a particular group. All of the methods of these groups are executed first before this method.
Syntax:
        @Test(dependsOnGroups={"GroupA", "GroupB", . . .})

6. How will you skip a particular test case in TestNG?
🅰 We can skip or disable a particular test case using enabled attribute. It can be achieved by setting enabled attribute of the Test annotation to false.
Syntax:
        @Test(enabled = false)

7. What is invocationCount in TestNG?
🅰 invocationCount in TestNG is an attribute that is used to execute a method or test in the number of times. It acts as a loop. 
For example:
        @Test(invocationCount = 5) Hence, this method will execute 5 times.

8. What is timeOut in TestNG?
🅰 timeOut in TestNG is an attribute that is used for a time out test. It specifies a time period (in milliseconds) to wait for a test for complete execution.

For more details go to link: https://www.scientecheasy.com/2019/06/testng-test-annotation.html

FAQs on TestNG XML File

1. What is testng.xml file?
🅰 Testng.xml file is a configuration file (XML file) for TestNG in which we can create test suites, test groups, mark tests for parallel execution, add listeners, and pass parameters to test script. It defines the runtime definition of a test suit.

2. What is the importance or use of testng.xml file?
🅰 There are following importance or usage of the testng.xml file in TestNG. They are as follows:
1. The testng.xml file can be used to control the execution of whole tests from a single file.
2. We can run a set of test cases from a single place.
3. We can pass parameters to test methods or classes.
4. Using testng.xml file, we can perform parallel test execution.
5. We can add the listener.

6. It defines the order of the execution of all the test cases.
7. It allows us to group test cases and can be executed as per requirements.
8. It helps to execute selected test cases.
9. With the help of testng.xml file, we can integrate TestNG framework with Jenkins.

3. How to pass parameters in test cases through testng.xml file?
🅰 We can pass the parameter value to test methods at runtime through the testng.xml file. For this, we can use @Parameter annotation:
Syntax:
        @Parameter("param-name");

4. What is thread-count in TestNG?
🅰 The thread-count in TestNG is an attribute that is used to run the maximum number of threads for each suite if the parallel mode is enabled (otherwise ignore it). 
For example, thread-count = "2": It will run your tests with two threads.

5. What is verbose in TestNG?
🅰 verbose is an attribute in TestNG which is mostly used when reporting a bug or when trying to diagnose the execution of test run.

6. How to create and run testng.xml file in Eclipse?