20 TestNG Interview Questions

Author: neptune | 17th-Dec-2022
#Selenium #Interview

TestNG framework is a testing framework to perform unit tests in the java programming language. Moreover, the "NG" in TestNG abbreviates for "Next Generation". 

Cedric Beust developed it and was inspired by the JUnit and NUnit testing frameworks. In addition to all the features in JUnit, TestNG has new features which make it more powerful.

Q. 1 What is TestNG?

TestNG is the framework created for executing unit tests in java programs.

TestNG is also used by software testers to efficiently run the automated test scripts created in Selenium Webdriver.

Q. 2 How will you install TestNG in Eclipse?

Follow the below steps to install TestNG on Eclipse:

1. Go to Eclipse → Click on “Help” → Click on “Install New Software”.

2. Click on the “Add” button, Enter the name(Preferably TestNG) in the “Next” textbox. Enter “Location” in the textbox and click on the “OK” action button.

3. Check the TestNG checkbox and click on the “Next” action button. The installation will start and the Eclipse will restart after installation.

4. Right-click on the project in Eclipse → Select build path → Configure Build Path.

5. Select the library tab → Click on Add library button → Select TestNG → Click on Next → Click on Finish and Apply and close.

Q.3 How to run the TestNG script?

You can run the TestNG script as:

1. Right-click on the class in Eclipse, click on “Run as” and select “TestNG test”.


2. Directly click on the Run button on the toolbar of Eclipse.

Q.4 What are the annotations used in TestNG?

There are three sections of annotation in TestNG:

(i) Precondition annotations: 

These are the TestNG annotations that are executed before the test.





(ii) Test annotation: 

This is the annotation that is only mentioned before the test case (Before the method written to execute the test case)

@Test is the test annotation

(iii) Postcondition annotation:

These are the annotations that are executed after the test case. (After the method is written to execute the test case)





Q. 5 What is the sequence of execution of the annotations in TestNG?

The Sequence of execution of the annotations are:










Q. 6) What are the advantages of TestNG?

The advantages of TestNG are:

1. It is an open-source framework, hence it is easy to configure.

2. TestNG help in creating systematic test cases.

3. It has number of annotations which in turn makes the test case creation easy.

4. TestNG helps in defining priorities of the tests and the sequence of execution.

5. Grouping is possible using TestNG.

6. It generates HTML reports (Selenium Webdriver cannot generate the test reports alone).

7. Data parameterization is possible using TestNG.

8. In addition to all the functionalities of JUnit, TestNG has its functionalities, which in turn makes it more powerful.

Q. 7 How to set priorities in TestNG?

There is always more than one test or method in the class. If we do not prioritize these tests or methods, then the methods are selected alphabetically and executed while execution.

If we want to run the tests in the sequence we want, then we need to set the priority along with the @Test annotation.

This can be done as follows:

@Test (priority=1), @Test (priority=2)

Consider the following Example:

@Test (priority=2)

public void getText()





public void clickelement()




In the above example, clickelement() will get executed first as the priority is set to 1.

And, getText() will get executed after clickelement() as its priority is set to 2.

Q. 8 How to share the project report using TestNG?

There are a few ways to do so:

(i) After the execution of the TestNG class, there is one tab called “Result of running class” which is generated next to the console.

We can copy this and share it.

(ii) After the execution of the TestNG class,

  • Right-click on the project name and refresh

  • Click on the “Test-output” folder

  • Right-click on the “index.html” file and select the properties

  • Copy the link next to “Location”

We can share this link to see the basic HTML test report which is generated by TestNG.

This is the file that gets generated on your machine automatically after the execution of the class using TestNG.

Q. 9 How will you define grouping in TestNG?

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


Q. 10 What is a dependency on 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.

Hence, we will write as follows:


Public void homePageLaunched()



The above code shows that homePageLaunched() method is completely dependent on LoginTest() method.

If LoginTest() is passed, only then the homePageLaunched() method gets executed.

Q. 11 What is InvocationCount in TestNG?

If we want to execute a test case “n” a number of times, then we can use the invocationCount attribute as shown in the below example.



Public void print()



In the above example, the print() method will get executed 8 times.

Q. 12 What is timeOut in TestNG?

If any method in the script takes a long time to execute, then we can terminate that method using “timeout” in TestNG.

@Test(timeout = 5000)

In this case, the method will get terminated in 5000 ms (5 seconds) and the test case is marked as “Failed”.

Q. 13 How to handle exceptions in TestNG?

If there are some methods from which we expect some exceptions, then we can mention the exception in the @Test annotation so that the test case does not fail.


If a method is expected to have “numberFormatException” exception, then the test case will fail because of this exception if no try-catch block is specified.

But we can do it in TestNG by using “expectedException” attribute as follows.


Then the test case will run without failing.

Q. 14 What are the common TestNG assertions?

Common TestNG assertions include:

(i) Assert.assetEquals(String actual, String expected);

  • Accepts two strings parameters.

  • If both the strings are equal, the test case executes successfully otherwise the test case fails.

(ii) Assert.assertEquals(String actual, String expected, String message)

  • Accepts three strings parameters.

  • If both the actual and expected strings are equal, the test case executes successfully otherwise the test case fails.

  • The message is printed if the test case fails.

(iii) Assert.assertEquals(boolean actual, boolean expected)

  • It accepts two boolean values.

  • If both the boolean values are equal, the test case executes successfully otherwise the test case fails.

(iv) Assert.assertTrue(<condition(t/f)>)

  • It accepts a boolean value.

  • The assertion passes if the condition is True, else an assertion error is displayed.

(v) Assert.assertFalse(<condition(t/f)>)

  • It accepts a boolean value.

  • The assertion passes if the condition is False, else an assertion error is displayed.

(vi) Assert.assertTrue(<condition(t/f)>,message)

  • It accepts a boolean value.

  • The assertion passes if the condition is True, else an assertion error is displayed with the mentioned message.

(vii) Assert.assertFalse(<condition(t/f)>,message)

  • It accepts a boolean value.

  • The assertion passes if the condition is False, else an assertion error is displayed with the mentioned message.

Q. 15 How to disable a test in TestNG?

To disable a test in TestNG, we have to use the “enabled” attribute as follows:


Q. 16 What are the types of Asserts in TestNG?

To validate the results (pass/fail), we must use the assertion.

There are two types of assert in TestNG:

(i) Hard Assert:

Hard Assert is the normal assert used to do validations in the TestNG class.

We have to use the Assert class for hard assert as follows:

Assert.assertEquals(actual value, expected value);

If the hard assert fails, then none of the code gets executed after the assert statement.

(ii) Soft Assert:

If we want to continue the test execution even after the assert statement fails, then we have to use soft assert.

To create a soft assert, we have to create an object of a “softAssert” class as follows:

softAssert sassert = new softAssert();


So now if the test case fails, the execution is not terminated when we use soft assert.

Q. 17 How to pass parameters in the test case through the testng.xml file?

If we have a class in which a login method is defined, then we can pass the login parameters to this login method from the testing.xml file.

We will have to use the “@parameters” annotation as follows:



public void loginapp()






Now, go to the testng.xml file and enter the parameters there as follows:

<Suite name = “suitename”>

<test name =”testname”>

<parameter name =”user_name” value=”user1”/>

<parameter password =”password” value =”pass1”/>


<class name =”passingparameters”/>




Q. 18 What is the need to create a testng.xml file?

When we test a project using Selenium Webdriver, it has a lot of classes on it. We cannot choose these classes one by one and put them for automation. Hence we need to create a suite so that all the classes run in a single test suite.

We can achieve this by creating a testing.xml file.

Q. 19 How to create an XML file in TestNG?

Go to the src folder -> click on file ->enter the name of the file(mostly written testing.xml)

Then, Click on finish.

We have a blank XML file. Here, we have to mention the project name and the classes to be executed along with the package name as shown below.

<Suite name = "Testing Project">

<test name = "Testing feature">


<class name = "packagename.name of clasA"/>

<class name = "packagename.name of classB"/>

<class name = "packagename.name of classC"/>

<class name = "packagename.name of classD"/>




To run this file, we have to go to testng.xml in the package explorer right click, and run as → TestNG suite

Q. 20 How to throw a SKIP Exception in TestNG?

If we want to SKIP any Test using testing, then we have to use the SKIP exception in TestNG.

It is written as follows:

public void skipExc()


System.out.println("SKIP me");

throw new skipException(“Skipping skipExc”);



We wish you all the best for your interview!!!

Related Blogs
Automate Ticket Booking in SpiceJet from Delhi to Bengaluru using Selenium and Cucumber.
Author: neptune | 08th-Aug-2023
We are going to automate a ticket booking using Selenium WebDriver and Cucumber BDD...

Where you applied OOPs in Automation Testing?
Author: neptune | 28th-Aug-2023
#Interview #Java
You may face this question Where you have applied OOPs concept in Automation Framework? in almost all the Selenium Interviews. Let's learn OOP’s concept in Java before going further...

Selenium, Cucumber, JUnit, TestNG dependencies for Selenium project.
Author: neptune | 02nd-Apr-2023
#Selenium #Testing
We guide you how to update the pom.xml file for Selenium Maven project...

5 Selenium Project Ideas & for Beginners in Automation Testing
Author: neptune | 30th-Mar-2023
#Selenium #Testing #Projects
In this article, we will discuss 5 interesting Selenium project ideas for beginners in automation testing...

How to use wait commands in Selenium WebDriver in Java ?
Author: neptune | 22nd-Feb-2022
#Selenium #Testing #Java
We are going to explore different types of waits in Selenium WebDriver. Implicit wait, Explicit wait, and Fluent wait with examples...

Top 50+ Selenium Interviews Questions 2023 based on Years of Experience
Author: neptune | 02nd-Apr-2023
#Selenium #Testing #Interview
Every interview difficulty is based on how many years of experience you have in that field. For the Selenium Automation Tester I have divided the question on the number of years of experience...

Core Python Syllabus for Interviews
Author: neptune | 26th-Jul-2023
#Python #Interview
STRING MANIPULATION : Introduction to Python String, Accessing Individual Elements, String Operators, String Slices, String Functions and Methods...

Mostly asked Python Interview Questions - 2023.
Author: neptune | 30th-May-2023
#Python #Interview
Python interview questions for freshers. These questions asked in 2022 Python interviews...

Top 10 Selenium Interview Questions with answers (2021).
Author: neptune | 02nd-Apr-2023
#Selenium #Interview
In this article I will cover top 10 Selenium interview questions...

How to create Selenium Cucumber Project in Eclipse.
Author: neptune | 25th-May-2022
First, divide the process into the various steps to understand working in brief of a project. Steps in brief: We’ll start with initializing the browser driver and then log in to the web page...

30+ SQL Interview Questions
Author: neptune | 05th-Jan-2023
#Interview #SQL
Data Definition Language (DDL) – It allows end-users to CREATE, ALTER, and DELETE database objects...

Challenges faced in automating Web Applications using Selenium
Author: neptune | 02nd-Oct-2022
#Selenium #Testing
List of Challenges faced by testers using Selenium 1. Popup and Alert Handling, 2. Dynamic element Handling 3. Restricted to only Desktop Browsers Testing...

How to create a Selenium, Cucumber Automation project in Eclipse ?
Author: neptune | 02nd-Apr-2023
#Selenium #Testing
Problem Statement : Let’s consider this particular project where we will try automating the process of booking a flight ticket. Let’s get started and see how it’s done using Selenium...

Getting started with Selenium WebDriver.
Author: neptune | 02nd-Apr-2023
#Selenium #Testing
In this blog I will give you an overview of Selenium WebDriver and Also discuss about advantages and disadvantages of Selenium WebDriver...

Why we use Robot Class in Automation Testing?
Author: neptune | 23rd-Aug-2022
Robot class is used to generate native system input events for the purpose of automation testing. It is used to perform mouse and keyboard events on windows applications or popups...

Top 10 Selenium Interview Questions.
Author: neptune | 02nd-Apr-2023
#Selenium #Interview
Locator is a command that tells Selenium IDE which GUI elements (like Text Box, Buttons, Check Boxes etc) we are going to use or perform some automation task...

How to send POST request using Rest Assured framework in Java ?
Author: neptune | 26th-Mar-2023
#Selenium #API
Rest Assured is a popular Java-based framework that is used for testing RESTful web services. It provides a simple and intuitive API for sending HTTP requests and validating the responses...

Black Mirror Season 6: A Glimpse into the Future of Technology and Society
Author: neptune | 27th-Apr-2023
Black Mirror Season 6, starring Salma Hayek and Aaron Paul, promises more violence and thought-provoking explorations of technology and society...

The Key to QA Success: Understanding How Important Grooming Is?
Author: neptune | 19th-Sep-2023
#Testing #Interview
We will delve into the importance of grooming & ceremony for QA testers, key points to highlight their significance...

Top 20+ Appium Interview Questions and Answers (2023)
Author: neptune | 30th-May-2023
This article provides a comprehensive list of 20 common interview questions on Appium mobile automation, covering various topics and providing solutions for each question...

Skills Required for Full-Stack Developer at IBM Onsite, CA
Author: neptune | 25th-Feb-2024
#Interview #Jobs
The company's commitment to pushing the boundaries of what is possible necessitates a team of skilled professionals...

View More