Top TestNG Interview Questions (2024)
What is the TestNG?
What are the advantages of the TestNG?
Which are the different types of annotations used in the TestNG?
How the test script is run in the TestNG?
What is one of the main purposes of the TestNG?
How many types of Assert of TestNG?
What are SoftAssert in TestNG?
What are TestNG Test dependencies?
Which are the different types of Test Dependencies in the TestNG?
What is the TestNG test Suite?
Which are the attributes accepted in the suite?
What is the multiple test dependency in the TestNG?
What is the correct order of testng.xml tags from parent to child?
How testng.xml is created and Run?
What are the advantages of testng.xml?
What is the TestNG test group?
What are the TestNG test group uses?
What are the TestNG Listeners?
What is the TestNG Parameterization?
How many types of TestNG Parameters?
What are the uses of TestNG Parameters?
Why create the TestNG Parameters?
Which are the TestNG parameters that use the testng.xml file?
What is Parallel Test?
What are the uses of Parallel tests in the TestNG?
What are DataProviders in the TestNG?
How Data Providers are used in the TestNG?
How priority is set in the test case in TestNG?
How are test groups particular are excluded from a test case execution?
What is the difference between soft assertion and hard assertion?
What is the difference between Factory and DataProvider annotation?
What is the time unit we specify in test suites and test cases?
What is the use of @Test(invocationCount=x)?
What does the test timeout mean in TestNG?
TestNG Tutorial :
Q: What is the TestNG?
Ans:
- In TestNG, NG is for Next Generation. TestNG is an open-source automated TestNG framework.
- When actual development of the framework from scratch level is done TestNG framework plays a very important role.
- The limitations of the older framework were removed by the TestNG Framework.
- TestNG gave the ability to write flexible and significant tests to the developers with the help of annotations, sequencing, parametrizing, and grouping.
Q: What are the advantages of the TestNG?
Ans:
The advantages of the TestNG are as follow:
- The TestNG helps to define the dependency of one test over another test method.
- The TestNG helps to execute parallel test methods.
- Grouping of the test methods into the test group can be done with the help of TestNG.
- Priority to the test method can be assigned with the help of TestNG.
- The parameterizing of the test cases can be done by the TestNG feature of
@Parameter
annotation. - The reading of Excel files is possible with help of the TestNG data-driven test. It is denoted
as
@DataProvider
annotation. - The actual and expected result can be checked with the TestNG Assertions.
- Multiple tests are done by using the testng.xml file in the TestNG.
Q: Which are the different types of annotations used in the TestNG?
Ans:
The different types of annotations according to order in which they are used in the TestNG are:
@BeforeSuite
@BeforeTest
@BeforeClass
@BeforeMethod
@Test
@AfterSuite
@AfterTest
@AfterClass
@AfterMethod
Q: How the test script is run in the TestNG?
Ans:
The test script in the TestNG is run by right click-> on the TestNG class-> Run As and the test is Run.
Q: What is one of the main purposes of the TestNG?
Ans:
TestNG main goal is to cover tests on a wider range with more significant and easy processes for functional, unit, integration end to end, etc.
Q: How many types of Assert of TestNG?
Ans:
TestNG Assert has two types as given below:
Hard Assert
Hard Asserts are asserts that terminate the test execution if an assert statement fails, and thus do not validate further assert statements.
Example:Assert.assertEquals("user1", "user2", "validate the user");
Soft Assert
In SoftAssert, if one asserts validation fails, further assertions continue to execute, in other words, the test execution does not come to a halt. Soft Assert is opposite to Hard Assert.
Q: What are SoftAssert in TestNG?
Ans:
SoftAssert is also one type of Assertion. Even though one asserts validation fails in soft asserts, the other assertions keep on running due to this implementation of the test do not stop.
In TestNG soft assert are not included by default.
In simple words, SoftAssert means a test method to continue implementation even after the failure of the first assertion. When there are many multiple assertions or after the assertion statement want to implement some other line of codes in the test method then this type of requirement arises.
TestNG provides a SoftAssert class for dealing with all these types of cases.
Syntax:
SoftAssert softAssert = new SoftAssert()
Q: What are TestNG Test dependencies?
Ans:
We are often required to run tests in a particular order and even one test should run only when another test is run in TestNG is called Test Dependencies. It is a test method to depend on a single or group of the test methods in the TestNG.
Test Dependency will help in implementing a group of tests to be implemented before a test method. Tests dependency is only applicable if depend on method is a part of any inherited base class or part of the same class.
Q: Which are the different types of Test Dependencies in the TestNG?
Ans:
Following are the different types of Test Dependencies in TestNG are:
- Single Dependent Test Methods
- Multiple Dependent Test Methods
- Inherited Dependent Test Methods
- Group Dependent Tests In TestNG
Q: What is the TestNG test Suite?
Ans:
A test suite of software programs is a collection of test cases that are used to test a behavior or a set of behaviors. As suite is the feature of execution it is represented by an XML file and not defined in the testing source code in testNG.
The tests are run with a flexible configuration. A suite can consist of one or more tests. The suite
is interpreted as suite
. The foundation of testng.xml is suite
.
A test suite is described from it and many test
sections are made from it.
Q: Which are the attributes accepted in the suite?
Ans:
Following are attributes with their descriptions that accept the
<suite>
:
- Name: The compulsory attribute is the name of the suite.
- parallel: Parallel will help TestNG to decide whether it should run different threads to run this suite.
- Annotations: In your tests type of annotations is used.
- verbose: The level for the run.
- Thread count: The number of threads that are used. No need to count thread if Parallel mode is selected.
- Time count: The time used for completing all the test methods found in this test.
Q: What is the multiple test dependency in the TestNG?
Ans:
As a part of the dependency support, Multiple Dependent Test Method is a very well-supported quality by TestNG. Sometimes for the test method to depend upon multiple other methods is required.
Q: What is the correct order of testng.xml tags from parent to child?
Ans:
The correct order of testng.xml tags from parent to child
<suite>
<test>
<classes>
<class>
<methods>
Q: How testng.xml is created and Run?
Ans:
In the TestNG, the testng.xml file is created to create and run multiple test classes. TestNG.xml file is a configuration file that helps to keep the tests in an organized manner. Multiple test classes, tests, define test suites can be created and handled by using testng.xml.
The testng.xml is the main feature of the TestNG because without it the work in TestNG would become difficult. It makes it easy to control the execution of tests by keeping all the test cases together.
Q: What are the advantages of testng.xml?
Ans:
The advantages of the testng.xml are as follow:
- The parallel test method can be executed.
- One test method is dependent on another test method.
- A grouping of the test methods in the test group is done using testng.xml.
- Test methods are prioritized.
- Using
@DataProviders
annotation data-driven test can be done. - Using
@Parameters
annotation the parameterization of the test cases can be supported. - The validation of the expected results with the actual results is done by using testng.xml for different types of assertions.
- Logs can be created with the help of Listeners in it.
Q: What is the TestNG test group?
Ans:
In the TestNG, the tests are combined into groups is called the TestNG group. In the TestNG test group, the tester is allowed to select and run the test which one wants to Run or ignore. In TestNG different groups are combined, can use regular expression, etc.
Q: What are the TestNG test group uses?
Ans:
Using the TestNG test group maximum flexibility is achieved by the test easy and simple. One of the main useful features is not only the methods are declared inside a specified group but even another group inside a specified group can be declared.
Q: What are the TestNG Listeners?
Ans:
TestNG Listeners is one of the main features of the TestNG framework. It works as an interface that listens to events in the test scripts which are already defined and modifies the TestNG tool default behavior. TestNG listeners help to customize reports or create logs for particular tests. TestNG helps with the clean-up process after the testing is finished.
The TestNG Listeners are denoted as @listeners
annotation. TestNG listeners are defined
either
before or after the test case.
The TestNG listeners are known are ITestListener
.
Q: What is the TestNG Parameterization?
Ans:
The TestNG Parameterization is one of the features of TestNG provides one can run the same test over
and over again with different values. In simple words, TestNG Parameters are the arguments that are
implemented in the test methods. It is denoted as @Parameters
.
Q: How many types of TestNG Parameters?
Ans:
There are two types of TestNG Parameters, they are:
- TestNG Parameters using Testng.xml file.
- TestNG Parameters using data providers.
Q: What are the uses of TestNG Parameters?
Ans:
In TestNG parameters are used as numbers of test cases are run with multiple input values so for keeping the test case result successfully. Every time a Hardcode test is not possible the TestNG Parameterization is helpful in testing because if one change is required, the whole test script is to be edited.
TestNG Parametrization use makes the testing easy and makes it in a more organized form.
Q: Why create the TestNG Parameters?
Ans:
There are some values that are constant in all the test cases but when to set values for username, login id, password the TestNG Parameters are used.
Q: Which are the TestNG parameters that use the testng.xml file?
Ans:
In testng.xml the value of the parameter is automatically converted by the TestNG. The following values are converted by the TestNG:
- Integer
- string
- Byte
- Boolean
- Double
- Character
- Float
- Long
- short
Q: What is Parallel Test?
Ans:
Parallel: The test method will receive the value from the data provider will run in parallel if this is set as true. The default value is false.
Syntax of parallel: Parallel tests, methods, and classes use parallel attributes on the <suite>
.
Q: What are the uses of Parallel tests in the TestNG?
Ans:
The uses of Parallel tests in the TestNG are:
- The time of the test is reduced.
- Multi-thread tests are allowed.
Q: What are DataProviders in the TestNG?
Ans:
Data Providers are one of the parameters used in the TestNG. In TestNG data providers are used to pass the parameters in the test function. In the TestNG Test case in a single implementation and in the form of TestNG Annotations different values are passed by using Data providers.
The data provider is a part of data-driven testing in the TestNG. Different parameters are passed using data providers.
Q: How Data Providers are used in the TestNG?
Ans:
The data provider method is a set as a separate process from a test method. In the TestNG @DataProvider annotation parameter is given below:
- name: A particular data provider name is highlighted. This name is used further with the @Test annotated method which wants to receive data from the @DataProvider. If the parameter name is not given in @DataProvider, then the name of this data provider will choose on its own and set as the name of the method.
- Parallel: The test method will receive the value from the data provider will run in parallel if this is set as true. The default value is false.
- Syntax of parallel: Parallel tests, methods, and classes use parallel attributes on the
<suite>
.
Q: How priority is set in the test case in TestNG?
Ans:
The priority
attribute is used for the @Test
annotations. If the priority
is not set then it will execute the test scripts in alphabetical order.
package com.techgeeknext.testcases;
import org.testng.annotations.Test;
public class LoginTestCases {
@Test(priority = 1)
public void testConnectDB() {
System.out.println("Test Connection.");
}
@Test(priority = 2)
public void testAuthenticateUser() {
System.out.println("Test Authenticate User.");
}
}
Q: How are test groups particular are excluded from a test case execution?
Ans:
By adding the exclude tag in the testng.xml
a particular test group is excluded from a
test case execution.
<groups>
<run>
<exclude name="TestGroupNameToExclude"/>
</run>
</groups>
Q: What is the difference between soft assertion and hard assertion?
Ans:
Soft Assertion
If the TestNG gets an error it will throw an exception during @Test when the assertion is failed and go on with the next statement after the assert statement.Hard Assertion
If TestNG gets an error it will throw an AssertExpection during @Test when an assertion fails and stops execution after the assert statement.Q: What is the difference between Factory and DataProvider annotation?
Ans:
Factory
It is denoted as@Factory
. It is implemented for the test method present
in the same test class using different instances of the respective class.
DataProvider
It is denoted as@DataProvider
. It is implemented for the test method
multiple numbers on times based on the data provided by the DataProvider.
Q: What is the time unit we specify in test suites and test cases?
Ans:
We specify the time unit in test cases and the test suites are in milliseconds.
Q: What is the use of @Test(invocationCount=x)?
Ans:
The invocation count attribute tells how many times TestNG should run a test method
@Test(invocationCount = 10)
public void validateData(){
Q: What does the test timeout mean in TestNG?
Ans:
The maximum time a test case should take in milliseconds.
@Test(threadPoolSize =4 , invocationCount = 11, timeOut = 10000)
public void authenticateUser(){