TestNG Questions and Answers for Selenium Interview

 

  1. What is TestNG?

  • TestNG is a Java testing framework, inspired by JUnit and NUnit.
  • NG stands for Next Generation.
  • TestNG is designed to cover unit, functional, end-to-end, integration, etc.,

 

  1. What are the advantages of TestNG?

  • TestNG provides parallel execution of test methods.
  • It allows to define dependency of one test method over other method.
  • It allows to assign priority to test methods.
  • It allows grouping of test methods into test groups.
  • It has support for parameterizing test cases using @Parameters annotation.
  • It allows data driven testing using @DataProvider annotation.
  • It has different assertions that helps in checking the expected and actual results.
  • Detailed (HTML) reports.

 

  1. What are the annotations available in TestNG?

  • @BeforeTest
  • @AfterTest
  • @BeforeClass
  • @AfterClass
  • @BeforeMethod
  • @AfterMethod
  • @BeforeSuite
  • @AfterSuite
  • @BeforeGroups
  • @AfterGroups
  • @Test

 

  1. What is the importance of testng.xml file?

Using testng.xml file configure the complete test suite in a single file.

  • The testng.xml file allows to include or exclude the execution of test methods and test groups.
  • It allows to pass parameters to the test cases.
  • Allows to add group dependencies.
  • Allows to add priorities to the test cases.
  • Allows to configure parallel execution of test cases.
  • Allows to parameterize the test cases.

 

  1. How to pass parameter through testng.xml file to a test case?

  • We define the parameters in the testng.xml file and then reference those parameters in the source files.
  • Create a java test class, say, ParameterizedTest.java and add a test method say parameterizedTest() to the test class.
  • This method takes a string as input parameter. Add the annotation @Parameters(“browser”) to this method.

The parameter would be passed a value from testng.xml.

 

  1. What is TestNG Assert and list out common TestNG Assertions?

  • TestNG Asserts help us to verify the condition of the test in the middle of the test run.
  • Based on the TestNG Assertions, we will consider a successful test only if it is completed the test run without throwing any exception.

 

  1. assertEqual(String actual,String expected)
  2. assertEqual(String actual,String expected, String message)
  3. assertEquals(boolean actual,boolean expected)
  4. assertTrue(condition)
  5. assertTrue(condition, message)
  6. assertFalse(condition)
  7. assertFalse(condition, message)

 

  1. What is Soft Assert in TestNG?

  • Soft Assert collects errors during @Test.
  • Soft Assert Execute the remaining test case even after an assertion fails.

 

  1. What is Hard Assert in TestNG?

  • Hard Assert throws AssertException immediately, test is marked as failed and test suite continues with next @Test.

 

  1. What is exception test in TestNG?

  • You can test whether a code throws a desired exception or not.
  • Here the expectedExceptions parameter is used along with the @Test annotation.

E.g:

 

  1. How to set test case priority in TestNG?

  • We use priority attribute to the @Test annotations.
  • In case priority is not set then the test scripts execute in alphabetical order.

Output:

 

  1. What is parameterized testing in TestNG?

  • Using Parameterized testing allow developers to run the same test over and over again using different values.

 

  1. How can we create data driven framework using TestNG?

  • By using @DataProvider annotation we can create a Data Driven Framework.

 

  1. How to run a group of test cases using TestNG?

  • One test able to assigned multiple groups using “groups” in TestNG.
  • Groups are specified in your testng.xml file and can be found either under the <test> or <suite> tag.
  • Groups specified in the <suite> tag apply to all the <test> tags underneath.

 

  1. How to create Group of Groups in TestNG?

  • Groups can also include other groups. These groups are called MetaGroups.

Add groups into TestNG.xml file to Execute.

 

  1. How to run test cases in parallel using TestNG?

Using “parallel” attribute in testng.xml to accomplish parallel test execution in TestNG.

The parallel attribute of suite tag can accept four values:

  • tests– All the test cases inside <test> tag of testng.xml file will run parallel.
  • classes– All the test cases inside a java class will run parallel.
  • methods– All the methods with @Test annotation will execute parallel.
  • instances– Test cases in same instance will execute parallel but two methods of two different instances will run in different thread.

 

  1. How to exclude a particular test method from a test case execution?

  • By adding the exclude tag in the testng.xml:

 

  1. How to exclude a particular test group from a test case execution?

  • By adding the exclude tag in the testng.xml we Skip / Exclude test from TestNG.xml file.

 

  1. How to disable / ignore a test case in TestNG?

  • Disable / Ignore the test case we use the parameter “enabled = false” to the @Test annotation.

 

  1. Different ways to produce reports for TestNG results?

  • Listeners implement the interface org.testng.ITestListener and are notified in real time of when a test starts, passes, fails, etc…
  • Reporters implement the interface org.testng.IReporter and are notified when all the suites have been run by TestNG. The IReporter instance receives a list of objects that describe the entire test run.

 

  1. What is the use of @Listener annotation in TestNG?

  • TestNG listeners are used to configure reports and logging.
  • One of the most widely used listeners in testNG is ITestListener interface.
  • It has methods like onTestStart, onTestSuccess, onTestFailure, onTestSkipped etc.

 

  1. What is the use of @Test(invocationCount=x)?

  • The invocationcount attribute tells how many times TestNG should run a test method.

  • The method method_name() will be invoked ten times.

 

  1. Explain what does @Test(invocationCount=?) and (threadPoolSize=?) indicates?

  • @Test (threadPoolSize=?): The threadPoolSize attributes tell TestNG to form a thread pool to run the test method through multiple threads. With threadpool, the running time of the test method reduces greatly.
  • @Test(invocationCount=?): The invocationcount tells how many times TestNG should run this test method.

 

  1. How to skip a @Test method from execution?

  • Using SkipException inside @Test method to skip a test case from test execution.

  • It will throw skip exception and @Test method will be ignored immediately from execution.

 

  1. Why soft assertion is used in Selenium WebDriver and TestNG automation project?

  • Using TestNG soft assertion continue the test execution even if the assertion is failed.
  • That means once the soft assertion fails, remaining part of <@Test> method is executed and the assertion failure is reported at the end of <@Test> method.

 

  1. Basic steps for drafting TestNG test cases?

  • Write down the business logic of your test.
  • Add appropriate TestNG annotations in your code.
  • In <build.xml> or <testing.xml>, add the information about your test.
  • Run your TestNG project.

 

  1. What are the different attributes for @Test annotation?

  • alwaysRun, dataProvider, description, enabled, expectedExceptions, groups, priority, timeOut

TestNG Questions and Answers for Selenium Interview and TestNG Questions and Answers for Selenium Interview and TestNG Questions and Answers for Selenium Interview and TestNG Questions and Answers for Selenium Interview