Register Login

TestNG Interview Questions & Answers

Updated Mar 27, 2019

What is TestNG?

TestNG is a tool to perform tasks like reporting, assertions, parallel test execution etc. integrating the framework with selenium or other automation tools. The NG in TestNG stands for next generation.

Why do we use TestNG in Selenium?

Bu using TestNG in Selenium you can generate test results in a proper format, that is not possible in the standard version of Selenium. You will also get a report stating the number of tests that were successful, the number of tests that failed and the number of tests that could not be executed. You can run the same test more than once without using loops. Groping of tests and conversion into .xml files also become easy.

TestNG Interview Questions & Answers

What are some key features of TestNG?

The key features of TestNG include -

  • Supports reliant methods used to run tests
  • Support for the various parameters
  • Flexible test structure
  • Embedded BeanShell assures greater flexibility
  • Supports execution models that are powerful
  • Default runtime functions
  • Supports helpful annotations such as @BeforeSuite, @AfterSuite, @BeforeClass, @AfterClass, etc
  • Supports multiple instances for an identical test class
  • Parameterized tests are handled in a more sophisticated manner using concepts of data-provider
  • Tools and plug-ins like Maven, Eclipse, IDEA etc. ensure extendibility

How to set priority in TestNG?

The priority of a test can be set in TestNG with the help of the parameter "priority" in the @Test annotation. The test that has a lower value of priority is executed before a test with a higher priority. Example-


How to run failed test cases in TestNG?

A test can fail in TestNG for several reasons like issues of the network, system or browser. However, there are 2 ways in which the failed test can be executed.  In a first way, you have to right-click Project and select Refresh. A folder named “test-output” is generated where you will find testng-failed.xml”. Running this file will help you run failed test cases again. You can also create a class to use IRetryAnalyzer.

How to skip test in TestNG?

To skip the execution of a test in TestNG, you can use the SkipException() parameter. With this parameter, you can deliberately skip a test if a certain condition is met.

What is data provider in TestNG?

@DataProvider is an annotation that transfers a test data to the test method. The test method will execute the test based on the total number of rows of data that were transferred by the data provider.

What is @test in TestNG?

@test is an annotation in TestNG that helps us to mark the method of execution as Test Method.

What is data provider in TestNG?

@DataProvider is an annotation that transfers a test data to the test method. The test method will execute the test based on the total number of rows of data that were transferred by the data provider.

What is the test suite in TestNG?

A test suite is a group of tests that check the behaviour or a group of behaviours projected by a software program. In TestNG, you cannot use a testing source code to define a suite. However, you can use a .xml file. The main advantage of the test suite is that it enables you to flexibly configure a test that must be run. A test suite, defined by the <suite> tag, can contain more than one test.

What is an annotation in TestNG?

Annotation in TestNG is used to identify the method of execution of a test. The execution can be modified by introducing parameters. The common annotations include-

  • @Test- It identifies the method of execution as Test method.
  • @BeforeSuite- This will run for a single time before any other tests in a suite.
  • @AfterSuite- This will run once after all other tests in a suite.
  • @BeforeClass- This method is executed once before the first test in an invoked class.
  • @AfterClass-  This method is executed after all the tests in an invoked class.
  • @BeforeTest- This method will run before any other test method in any class inside the executed <test> tag.
  • @AfterTest- This method will run after all the other test method in any class inside the executed <test> tag.
  • @DataProvider- It passes test data to test method.

What is ITestResult in TestNG?

ITestResult in TestNG helps you to customise the logs and reports of a test. This will help you understand which of the tests are a success, the ones that fail and those that are skipped.

How to create .xml file in TestNG?

A .xml file is necessary for TestNG to generate and manage numerous test classes. You can create a .xml file you will have to right click on Project. Select ‘File’ from New File window. Add the file name with .xml extension and click Finish.

How to generate HTML report using TestNG?

To generate an HTML report in TestNG, you can use the ReportNG plug-in that generates a simple to read and colour-coded test results.

What is TestNG M2E integration

TestNG M2E Integration is a plug-in that enables the user to run tests with System Properties or JVM settings defined by maven-surefire-plugin or maven-failsafe-plugin of pom.xml.

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. Higher the value you will use, more detailed will be the test results log in Eclipse IDE -> 'console' window.

What is the difference between JUnit and TestNG

Read here Difference between JUnit and TestNG with Comparison Chart

How is TestNG better than JUnit?

There are several features in TestNG that are not present in JUnit. The advantages of TestNG over JUnit are-

  • Annotations in TestNG are easier than JUnit
  • Constraints like @BeforeClass and @AfterClass are not present in TestNG unlike JUnit
  • There is no method name constraint in TestNG like there is in JUnit
  • Grouping of test cases is possible in TestNG
  • There are 3 setUp/tearDown levels in TestNG: @Before/AfterSuite, @Before/AfterTest and @Before/AfterGroup
  • You can define dependent test cases where every test case is independent of others in TestNG
  • You can execute test cases in groups in TestNG
  • You can run multiple Selenium test cases in TestNG

What is invocationcount in TestNG?

InvocationCount is a parameter in TestNG that accepts integer values to run loops during a test execution. The integer indicates the number of times the loop will run.

What is parameterization in TestNG?

In TestNG a user can input values to test methods in the form of arguments. This is done by using parameter annotations in a testng.xml file. You can also pass values these values during runtime. This is known as parameterization. There are two attributes in a parameter tag. The first is the name attribute that defines the name of the parameter. The second one is the value attribute that defines the value of the parameter.