TestNG Interview QuestionsA list of top frequently asked TestNG Interview Questions and answers are given below. 1) What is TestNG?TestNG stands for "Testing Next Generation". It is an` automation testing framework used for java programming language developed by Credric beust, and it comes after the inspiration from the JUnit framework. TestNG consists of all the features of JUnit framework but also contains some more additional features that make TestNG more powerful. 2) What are the advantages of TestNG?The following are the advantages of TestNG are:
3) 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". 4) What are the annotations used in the TestNG?The following are the annotations used in the TestNG are:
5) 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:
6) How to set the priorities in TestNG?If we do not prioritize the test methods, then the test methods are selected alphabetically and executed. If we want the test methods to be executed in the sequence we want, then we need to provide the priority along with the @Test annotation. Let's understand through an example. 7) Define grouping in TestNG?The group is an attribute in TestNG that allows you to execute the 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. Let's understand through an example. testng.xml 8) What is dependency in TestNG?When we want to run the test cases in a specific order, then we use the concept of dependency in TestNG. Two types of dependency attributes used in TestNG:
9) 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. TimeOut in TestNG allows you to configure the time period to wait for a test to get completely executed. It can be configured in two levels:
The timeOut attribute can be specified as shown below: The above @Test annotation tells that the test method will be given 700 ms to complete its execution otherwise it will be marked as a failed test case. 10) What is invocationCount in TestNG?An invocationCount in TestNG is the number of times that we want to execute the same test. Output 11) What is the importance of testng.xml file?The testng.xml file is important because of the following reasons:
12) How to pass the parameter in test case through testng.xml file?We can also pass the value to test methods at runtime, we can achieve this by sending parameter values through the testng.xml file. We can use the @Parameter annotation: Let's understand through an example: testng.xml file On running the testng.xml file, we get the output as shown below: 13) How can we disable the test case from running?We can disable the test case from running by using the enabled attribute. We can assign the false value to the enabled attribute, in this way we can disable the test case from running. 14) What is the difference between soft assertion and hard assertion?Soft Assertion: In case of Soft Assertion, if TestNG gets an error during @Test, it will throw an exception when an assertion fails and continues with the next statement after the assert statement. Hard Assertion: In the case of Hard Assertion, if TestNG gets an error during @Test, it will throw an AssertException immediately when an assertion fails and stops execution after the assert statement. Let's understand through an example. Output 15) What is the use of @Listener annotation in TestNG?TestNG provides different kinds of listeners which can perform different actions whenever the event is triggered. The most widely used listener in TestNG is ITestListener interface. The ITestListener interface contains methods such as onTestSuccess, onTestfailure, onTestSkipped, etc. Following are the scenarios that can be made:
Let's understand through an example. Listener.java Output 16) What is the use of @Factory annotation?The @Factory annotation is useful when we want to run multiple test cases through a single test class. It is mainly used for the dynamic execution of test cases. Let's understand through an example. testcase1.java testcase2.java Factory.java 17) What is the difference between @Factory and @DataProvider annotation?@DataProvider: It is annotation used by TestNG to execute the test method multiple numbers of times based on the data provided by the DataProvider. @Factory: It is annotation used by the TestNG to execute the test methods present in the same test class using different instances of the respective class. |