Top TestNG Interview Questions & Answers

TestNG Interview Questions & Answers
Table of Contents

1. What is TestNG?

TestNG stands for Testing Next Generation. It is an automation testing framework used for Java programming language. It was developed by Credric Beust, and it comes after inspiration from the JUnit framework.

2. What are the important features of TestNG?

  • TestNG uses more OO (object-oriented) and Java features
  • Different Annotations are supported
  • It provides different types of assertions that help in checking actual and expected results.
  • It provides priority and grouping features
  • TestNG allows Data-driven testing
  • TestNG internally generates an HTML test report

3. What are the basic steps required in writing the TestNG test?

Three basic steps are required to write a test case.

  • Write down the business logic of the test and annotate it with TestNG annotations.
  • Create a testing.xml file and add the information about your test
  • Run TestNG.

4. How to run a test script in TestNG?

We can run a test script in TestNG by right-clicking on the TestNG class, clicking on the “Run As” option, and then selecting “TestNG test”.

5. What are Annotations in TestNG?

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

6. What are the annotations used in the TestNG?

The following annotations used in the TestNG are:

  • Precondition annotations: Precondition annotations are executed before the execution of test methods The Precondition annotations are @BeforeSuite, @BeforeClass, @BeforeTest, and @BeforeMethod.
  • Test annotation: Test annotation is specified before the definition of the test method. It is specified as @Test.
  • Postcondition annotations: The postcondition annotations are executed after the execution of all the test methods. The postcondition annotation can be @AfterSuite, @AfterClass, @AfterTest, or @AfterMethod.

7. What is the sequence of execution of all the annotations in TestNG?

The sequence of execution of all the annotations in TestNG is given below

  • @BeforeSuite
  • @BeforeTest
  • @BeforeClass
  • @BeforeMethod
  • @Test
  • @AfterSuite
  • @AfterTest
  • @AfterClass
  • @AfterMethod

8. What is the 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.
  • 9. What is the test method?

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

    10. How to set priorities in TestNG?

    If we do not prioritize these tests or methods, then the methods are selected alphabetically and executed while execution. We can set priorities like

    @Test(priority=1), @Test(priority=2)
    testNG interview questions and answers

    11. In TestNG how can you disable a test?

    To disable the test case you don’t want, you can use annotations

    @Test(enabled = false)

    12. Define TestNG Groups.

    The group is an attribute in TestNG that allows you to execute multiple test cases. For example, if we have 100 test cases of it_department and 10 test cases of hr_department, and if you want to run all the test cases of it_department together in a single suite, this can be possible only through the grouping.

    13. How will you define grouping in TestNG?

    We can define grouping in TestNG using the groups attribute as shown below:

    @Test(groups=”title”)

    14. What is dependency in TestNG?

    There are some methods on which many methods are dependent.

    For Example, if we want to test any application, and if the login page of the application is not working then we won’t be able to test the rest of the scenarios.

    So, LoginTest is the method on which many tests are dependent. If LoginTest() is passed, only then does the other method get executed.

    15. How to write a dependency test in TestNG?

    Two ways to write dependency tests. Dependency can be on methods or groups. If any method depends upon another method, mention the method name to show dependency.

    @Test(dependsOnMethods=”LoginTest”) 

    If any method depends upon any group, mention the group name to show dependency.

    @Test(dependsOnGroups=”functional”)

    16. 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.

    @Test(groups = { "GroupName1", "GroupName2", "GroupName3" .... })

    17. How do you exclude a group from the test execution cycle?

    The exclusion of a group in TestNG indicates that this group will not run throughout the execution and will be ignored by TestNG. Furthermore, the name of the group to be excluded is defined in the XML file using the following syntax:

    <groups>  <run>
       <exclude name="groupname"> </exclude>
    </run>  </groups> 

    18. What is timeOut in TestNG?

    While running test cases, there can be a case when some test cases take much more time than expected. In such a case, we can mark the test case as a failed test case by using timeOut.

    @Test(timeOut = 700)

    Here, the test method will be given 700 ms to complete its execution otherwise it will be marked as a failed test case.

    19. What is the 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 means two methods run parallel in threads.

    20. What is a testng.xml file?

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

    testNG interview questions and answers

    21. What is the importance of the testng.xml file?

    • It defines the order of the execution of all the test cases.
    • It executes the selected test cases.
    • It allows you to group the test cases and can be executed as per the requirements.
    • It will run multiple test cases at once.

    22. What is Parameterization in TestNG?

    Parameterization is a feature provided by TestNG that allows the users to pass parameter values to test methods as arguments.
    Using this feature, we can run the same test over and over again with different values. The parameterization test is supported by using Parameter and DataProvider annotations of TestNG.

    23. How many ways by which we can pass parameter values to test methods?

    There are mainly two ways through which we can directly pass parameter values to test methods.

    • Through testng.xml file.
    • Through DataProviders

    24. How to run the test script in TestNG?

    You can run the test script in TestNG by clicking right click on the TestNG class, click on "Run As" and then select "TestNG test".

    25. How to fail a testNG test if it doesn’t get executed within a specified time?

    We can use the timeOut attribute of @Test annotation. The value assigned to this timeOut attribute will act as an upper bound. If the test doesn’t get executed within this time frame then it will fail with timeout exception.

    26. Name various methods to execute tests in TestNG?

    Using XML Suite Files: You can define test suites and tests in an XML file. This method provides the most flexibility in terms of test execution sequence, grouping, and parameterization.

    Using Annotations: You can annotate methods with @Test to signify they're test methods. Then you can run these directly from an IDE.

    27. What is the difference between a test method and a configuration method in TestNG?

    In TestNG, a test method is a method that contains the actual test logic, while a configuration method is a method that is used to set up or tear down the test environment. Configuration methods include: @BeforeSuite, @BeforeClass etc

    28. What is Group of Groups in TestNG?

    Groups can also include other groups. These groups are called MetaGroups. For example, you might want to define a group all that includes smokeTest and functionalTest.

    <groups>
          <define name="all">
             <include name="smokeTest"/>
            <include name="functionalTest"/>
          </define>
          <run>
            <include name="all" />
          </run> 
        </groups>
    

    29. How to pass the parameter in the test case through the testng.xml file?

    You can use the parameter tag within the test tag to pass parameters in the test case via the TestNG XML file. First, you need to specify the parameter name and value within the tag. These parameters can be accessed in the test case by using the @Parameters annotation in the method

    @Test
    @Parameters({"username", "password"})
    public void myTestMethod(String username, String password) {
    //code
    }

    Now use the XML file to pass the values to the parameters "username" and "password":

    // TestNG XML file
      <test name="MyTest">
        <parameter name="username" value="myname" />
        <parameter name="password" value="123" />
        <classes>
          <class name="MyTestClass" />
        </classes>
      </test>
    </suite> 

    30. What is @Factory annotation in TestNG?

    The @Factory annotation is used to create test instances at runtime. It enables you to generate test classes or instances dynamically based on runtime conditions or parameters.
    You can use the @Factory annotation to create multiple instances of the same test class with different data sets or parameters

    testNG interview questions and answers

    31. What is the difference between @Factory and @Dataprovider annotations?

    The @Factory annotation is used to create multiple instances of the same test class, each with a different set of input parameters or configurations. This is useful when running the same test with different data sets or configurations or when running tests in parallel to save time during testing.

    @Factory
    public Object[] factoryMethod() {
    return new Object[] {   
    new SimpleTest("one"),
    new SimpleTest("two")	
    };
    }

    Meanwhile, @DataProvider annotation supplies test data to a test method. It allows you to separate the test data from the test logic, making your test code easier to maintain and reuse.

    @DataProvider
    public Object[][] dataMethod() {
    return new Object[][] { { "one" }, { "two" } };
    }

    32. What is Assertion in TestNG?

    Assertion assists us in determining whether the expected and actual results are equal. To determine whether the test case passed or failed, we use the built-in "Assert" class and many of its methods in TestNG.

    33. Describe common TestNG assertions

    • assertEquals(expectedValue, actualValue, message): It compares two values and determines if they are equal.
    • assertTrue(condition, message): This assertion helps whether or not the specified condition is true.
    • assertFalse(condition, message): This assertion defines if the specified condition is true or false.

    34. What is the use of @Listener annotation in TestNG

    In TestNG, the @Listener annotation is used to define listeners, which are classes that listen to events that occur during the execution of a TestNG test. Listeners can be used to customize or enhance TestNG's behavior by adding extra functionality such as logging, reporting, or customizing the test execution flow.

    35. What are the types of reports generated in TestNG by default?

    After all of the test methods have been completed, TestNG generates several reports by default. Such as:

    • TestNG HTML reports
    • TestNG XML reports
    • TestNG index reports
    • Emailable reports

    36. Where is the emailable report generated and saved in TestNG?

    The emailable report is generated by default in the TestNG project's output directory, in the folder named "test-output." The emailable report's filename is "emailable-report.html."

    37. Where is the index report generated and saved in TestNG?

    The index.html report is one of TestNG's default reports, providing an overview of the test results as well as links to other reports such as the emailable report, testng-results.xml, and testng-failed.xml.

    The index.html report is generated by default in the TestNG project's output directory under the folder named "test-output." The index.html report's filename is "index.html."

    38. How to use the TestNG Reporter Class for log generation?

    The TestNG Reporter class is a built-in class for logging messages during test execution. Import the Reporter class and then pass a message string to the method as an argument as shown

    Reporter.log("Log message");

    39. What is meant by parallel test execution in TestNG?

    TestNG is designed to support the parallel execution of tests, which allows testers to run multiple tests concurrently, reducing overall test execution time.

    40. How to run Parallel Tests, Classes, & Methods in Selenium using TestNG XML File?

    We can run our test methods/classes/tests in parallel by using the “parallel” attribute for Test Suite in the testng.xml file. The parallel attribute for the tag can accept one of the following values – tests, classes, or methods.

    <suite name = "Parallel Test Suite" parallel = "methods"> 
    testNG interview questions and answers

    41. How to run test cases in parallel using TestNG?

    We can use the “parallel” attribute in testng.xml to accomplish parallel test execution in TestNG. The parallel attribute of the suite tag can accept four values:

    • tests - All the test cases inside <test> tag of testng.xml file will run parallel
    • classes - All the test cases inside a java class will run parallel
    • methods - All the methods with @Test annotation will execute parallel
    • instances - Test cases in the same instance will execute parallel but two methods of two different instances will run in different threads.

    42. What is the Time-Out test in TestNG?

    The Time-Out test in TestNG is nothing but the time allotted to perform unit testing. If the unit test fails to finish within that specific time limit, TestNG will abandon further software testing and mark it as a failure.

    43. What Is Factory Annotation In TestNG And Why Do You Use It?

    The “@Factory” as the name suggests is an annotation that can produce tests at runtime using the given data-sets/conditions. This annotation solves a common problem of executing tests with different datasets without explicitly creating them.

    Another advantage it brings is to allow the parameters to pass to test classes while instantiating them.

    44. How can we run test cases in parallel using TestNG?

    In order to run the tests in parallel just add these two key value pairs in the suite-

    Parallel states that what to run parallel whether it be methods/tests/classes. Thread count denotes the number of threads you want to run simultaneously.

    <suite name="TestingTestSuite" parallel="methods" thread-count="5"> 

    45. How do you create a TestNG test file?

    To create a TestNG test file, you need to create a Java class and add the necessary annotations and methods. Here's an example:

    import org.testng.annotations.Test; 
    public class MyTest { 
    @Test 
    public void testMethod() { 
    // Test code here 
     } }
    

    In this example, the class is named MyTest and contains a test method annotated with @Test. You can run this test file using TestNG by creating an XML file that specifies the test classes to run.

    46. How can you set a time limit for a test in TestNG?

    To set a time limit for a test in TestNG, you can use the @Test annotation's timeOut attribute and specify the time limit in milliseconds. For example:

    @Test(timeOut = 1000) 
    public void timedTestMethod() { 
    // code
     }
    

    47. What is verbose in TestNG?

    Verbose is an attribute in TestNG that finds application in the <suite> tag of the testng.xml configuration file. It can accept values from 1 to 10. The higher the value you will use, the more detailed will be the test results log in the Eclipse IDE -> 'console' window.

    48. How to exclude groups in TestNG?

    Mention the name of the group that we want to exclude is defined in the XML file by the following syntax:

    <groups>
      <run>
        <exclude name = "groupname"/>
       </run>
    </groups>
    

    49. What is the time unit we specify in test suites and test cases?

    We specify the time unit in test suites and test cases in milliseconds.

    50. What are the different ways to produce reports for TestNG results?

    TestNG offers two ways to produce a report.

    Listeners implement the interface org.testng.ITestListener and are notified in real time of when a test will start, pass, fail, etc…

    Reporters implement the interface org.testng.IReporter and are notified when all the suites have been run by TestNG. The IReporter instance receives a list of objects that describe the entire test run.

    not found

    Become a Master in Your Chosen Field by Earning Advanced Skills

    Best Learning, More Earning

    Proleed Academy

    Proleed serves / offers professionally designed IT training courses
    globally.

     

    Copyright © 2023 - Proleed Academy | All Rights Reserved.