Top TestNG Interview Questions (2024) | TechGeekNext

Top TestNG Interview Questions (2024)

  1. What is the TestNG?
  2. What are the advantages of the TestNG?
  3. Which are the different types of annotations used in the TestNG?
  4. How the test script is run in the TestNG?
  5. What is one of the main purposes of the TestNG?
  6. How many types of Assert of TestNG?
  7. What are SoftAssert in TestNG?
  8. What are TestNG Test dependencies?
  9. Which are the different types of Test Dependencies in the TestNG?
  10. What is the TestNG test Suite?
  11. Which are the attributes accepted in the suite?
  12. What is the multiple test dependency in the TestNG?
  13. What is the correct order of testng.xml tags from parent to child?
  14. How testng.xml is created and Run?
  15. What are the advantages of testng.xml?
  16. What is the TestNG test group?
  17. What are the TestNG test group uses?
  18. What are the TestNG Listeners?
  19. What is the TestNG Parameterization?
  20. How many types of TestNG Parameters?
  21. What are the uses of TestNG Parameters?
  22. Why create the TestNG Parameters?
  23. Which are the TestNG parameters that use the testng.xml file?
  24. What is Parallel Test?
  25. What are the uses of Parallel tests in the TestNG?
  26. What are DataProviders in the TestNG?
  27. How Data Providers are used in the TestNG?
  28. How priority is set in the test case in TestNG?
  29. How are test groups particular are excluded from a test case execution?
  30. What is the difference between soft assertion and hard assertion?
  31. What is the difference between Factory and DataProvider annotation?
  32. What is the time unit we specify in test suites and test cases?
  33. What is the use of @Test(invocationCount=x)?
  34. What does the test timeout mean in TestNG?

Q: What is the TestNG?
Ans:

  1. In TestNG, NG is for Next Generation. TestNG is an open-source automated TestNG framework.
  2. When actual development of the framework from scratch level is done TestNG framework plays a very important role.
  3. The limitations of the older framework were removed by the TestNG Framework.
  4. 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:

  1. The TestNG helps to define the dependency of one test over another test method.
  2. The TestNG helps to execute parallel test methods.
  3. Grouping of the test methods into the test group can be done with the help of TestNG.
  4. Priority to the test method can be assigned with the help of TestNG.
  5. The parameterizing of the test cases can be done by the TestNG feature of @Parameter annotation.
  6. The reading of Excel files is possible with help of the TestNG data-driven test. It is denoted as @DataProvider annotation.
  7. The actual and expected result can be checked with the TestNG Assertions.
  8. 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:

  1. @BeforeSuite
  2. @BeforeTest
  3. @BeforeClass
  4. @BeforeMethod
  5. @Test
  6. @AfterSuite
  7. @AfterTest
  8. @AfterClass
  9. @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:

  1. 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");
  2. 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:

  1. Single Dependent Test Methods
  2. Multiple Dependent Test Methods
  3. Inherited Dependent Test Methods
  4. 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>:

  1. Name: The compulsory attribute is the name of the suite.
  2. parallel: Parallel will help TestNG to decide whether it should run different threads to run this suite.
  3. Annotations: In your tests type of annotations is used.
  4. verbose: The level for the run.
  5. Thread count: The number of threads that are used. No need to count thread if Parallel mode is selected.
  6. 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:

  1. The parallel test method can be executed.
  2. One test method is dependent on another test method.
  3. A grouping of the test methods in the test group is done using testng.xml.
  4. Test methods are prioritized.
  5. Using @DataProviders annotation data-driven test can be done.
  6. Using @Parameters annotation the parameterization of the test cases can be supported.
  7. The validation of the expected results with the actual results is done by using testng.xml for different types of assertions.
  8. 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:

  1. TestNG Parameters using Testng.xml file.
  2. 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:

  1. Integer
  2. string
  3. Byte
  4. Boolean
  5. Double
  6. Character
  7. Float
  8. Long
  9. 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:

  1. The time of the test is reduced.
  2. 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:

  1. 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.
  2. 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.
  3. 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.");
	}
}
TestNG Priority

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(){

Recommendation for Top Popular Post :