Ques.1. What is testNG?
Ans. TestNG(NG for Next Generation) is a testing framework that can be integrated with selenium or any other automation tool to provide multiple capabilities like assertions, reporting, parallel test execution etc.
Ques.2. What are some advantages of testNG?
Ans. Following are the advantages of testNG-
Ques.3. What is the use of testng.xml file?
Ans. The testng.xml file is used for configuring the whole test suite. In testng.xml file, we can create test suite, create test groups, mark tests for parallel execution, add listeners and pass parameters to test scripts. We can also use this testng.xml file for triggering the test suite from command prompt/terminal or Jenkins.
Ques.4. What are some commonly used TestNG annotations?
Ans. The commonly used TestNG annotations are-
Ques.5. What are some common assertions provided by testNG?
Ans. Some of the common assertions provided by testNG are-
Ques.6. How can we disable or prevent a test case from running?
Ans. By setting "enabled" attribute as false, we can disable a test method from running.
Ques.7. How can we make one test method dependent on other using TestNG?
Ans. Using dependsOnMethods parameter inside @Test annotation in testNG we can make one test method run only after successful execution of dependent test method.
Ques.8. How can we set priority of test cases in TestNG?
Ans. We can define priority of test cases using "priority" parameter in @Test annotation. The tests with lower priority value will get executed first. Example-
Ques.9. What is the default priority of test cases in TestNG?
Ans. The default priority of test when not specified is integer value 0. So, if we have one test case with priority 1 and one without any priority then the test without any priority value will get executed first (as default value will be 0 and tests with lower priority are executed first).
Ques.10. How can we run a Test method multiple times in a loop(without using any data provider)?
Ans. Using invocationCount parameter and setting its value to an integer value, makes the test method to run n number of times in a loop.
Ques.11. What is threadPoolSize? How can we use it?
Ans. The threadPoolSize attribute specifies the number of thread to be assigned to the test method. This is used in conjunction with invocationCount attribute. The number of threads will get divided with the number of iterations of the test method specified in the invocationCount attribute.
Ques.12. What is the difference between soft assertion and hard assertion in TestNG?
Ans. Soft assertions (SoftAssert) allows us to have multiple assertions within a test method, even when an assertion fails the test method contiues with the remaining test execution. The result of all the assertions can be collated at the end using softAssert.assertAll() method.
Here, even though the first assertion fails still the test will continue with execution and print the message below the second assertion.
Hard assertions on the other hand are the usual assertions prodived by TestNG. In case of hard assertion in case of any failure, the test execution stops, preventing execution of any further steps within the test method.
Ques.13. How to fail a testNG test if it doesn't get executed within a specified time?
Ans. We can use timeOut attribute of @Test annotation. The value assigned to this timeOut attribute will act as an upperbound, if test doesn't get executed within this time frame then it will fail with timeOut exception.
Ques.14. How can we skip a test case conditionally?
Ans. Using SkipException, we can conditionally skip a test case. On throwing the skipException, the test method me marked as skipped in the test execution report and any statement after throwing the exception will not get executed.
Ques.15. How can we make sure a test method runs even if the test methods or groups on which it depends fail or get skipped?
Ans. Using "alwaysRun" attribute of @Test annotation, we can make sure the test method will run even if the test methods or groups on which it depends fail or get skipped.
Here, even though the parentTest failed, the dependentTest will not get skipped instead it will executed because of "alwaysRun=true". In case, we remove the "alwaysRun=true" attribute from @Test then the report will show one failure and one skipped test, without trying to run the dependentTest method.
Ques.16. How can we pass parameter to test script using testNG?
Ans. Using @Parameter annotation and 'parameter' tag in testng.xml we can pass paramters to test scripts.
Sample testng.xml -
Sample test script-
Ques.17. How can we create data driven framework using testNG?
Ans. Using @DataProvider we can create a data driven framework in which data is passed to the associated test method and multiple iteration of the test runs for the different test data values passed from the @DataProvider method. The method annotated with @DataProvider annotation return a 2D array of object.
Ques.18. What is the use of @Listener annotation in TestNG?
Ans. TestNG provides us different kind of listeners using which we can perform some action in case an event has triggered. Usually testNG listeners are used for configuring reports and logging. One of the most widely used lisetner in testNG is ITestListener interface. It has methods like onTestSuccess, onTestFailure, onTestSkipped etc. We need to implement this interface creating a listner class of our own. After that using the @Listener annotation we can use specify that for a particular test class our customized listener class should be used.
Ques.19. What is the use of @Factory annotation in TestNG?
Ans. @Factory annotation helps in dynamic execution of test cases. Using @Factory annotation we can pass parameters to the whole test class at run time. The parameters passed can be used by one or more test methods of that class.
Example - there are two classes TestClass and the TestFactory class. Because of the @Factory annotation the test methods in class TestClass will run twice with the data "k1" and "k2"
Ques.20. What is difference between @Factory and @DataProvider annotation?
Ans. @Factory method creates instances of test class and run all the test methods in that class with different set of data.
Whereas, @DataProvider is bound to individual test methods and run the specific methods multiple times.
Ques.21. How can we run test cases in parallel using TestNG?
Ans. In order to run the tests in parallel just add these two key value pairs in suite-
Check Running Selenium Tests in parallel for details.