TestNG

20 Most Popular TestNG Interview Questions and Answers:

20 Most Popular TestNG Interview Questions and Answers:

In this post, we will see TestNG Interview Questions with Answers. Our main focus is on Selenium TestNG Interview Questions and also we write some Selenium Interview Questions too.

Questions & Answers :

Question 1: What is TestNG?

TestNG is a testing framework designed to simplify testing needs, from unit testing to integration testing and system testing.

Question 2: Mention Few advantages of TestNG Framework?

  1. Parallel execution of test methods
  2. We can define the dependency of one test method over other methods
  3. Priority to test methods can be defined
  4. Test Methods can be grouped.
  5. Parameterization test cases can be done using @Parameters annotation
  6. Data-driven testing can be done using @DataProvider annotation
  7. Different Assertions can be provided.

Question 3: Mention Different types of Annotations in TestNG Framework?

Different types of Annotations in TestNG Framework are mentioned below:

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

Question 4: Mention correct sequence of tags in testng.xml from parent to child?

Below is the correct sequence of tags in testng.xml:

<suite>
<test>
<classes>
<class>
<methods>

Question 5: Explain the testng.xml file and its importance?

testng.xml file enables us to configure the complete test suite in a single file.

Importance
  1. It allows to include or exclude test groups
  2. It allows passing parameters to the test cases
  3. Allows to add group dependencies
  4. Allows to add priorities to the test cases
  5. Allows to configure parallel execution of test cases
  6. Allows to parameterize the test cases

Question 6: Mention common TestNG Assertions?

assertEqual(String actual,String expected)
assertEqual(String actual,String expected, String message)
assertEquals(boolean actual,boolean expected)
assertTrue(condition)
assertTrue(condition, message)
assertFalse(condition)
assertFalse(condition, message)

Question 7: What is the difference between Hard Assert and Soft Assert in TestNG?

  1. Hard Assert: Hard Assert throws an AssertException immediately when an assert statement fails and test suite continues with next @Test
  2. Soft Assert: Soft Assert collects errors during @Test. Soft Assert does not throw an exception when an assert fails and would continue with the next step after the assert statement.

Question 8: How can we define the parameters in the testng.xml file and then reference those parameters in the source files?

Create a java test class, say, Browser.java and add a test method say parametertest() to the test class. This method takes a string as input parameter. Add the annotation @Parameters(“browser”) to this method.

public class parametertest{
@Test
@Parameters("browser")
public void parameterizedTest(String browser){
if(browser.equals("firefox")){
System.out.println("Open Firefox Driver");
}else if(browser.equals("chrome")){
System.out.println("Open Chrome Driver");
}
} 
}

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

<parameter name="browser" value="chrome"/>
<parameter name="browser" value="firefox"/>

Question 9: How to set test case priority in TestNG?

We use priority attribute to the @Test annotations. In case priority is not set then it will be executed in alphabetical order.

public class PriorityTestCase{
@Test(priority=0)
public void testCase1() { 
system.out.println("Test Case 1");
}
@Test(priority=1)
public void testCase2() { 
system.out.println("Test Case 2");
}

Question 10: How to group of test cases using TestNG?

TestNG allows us to perform groupings of test methods.Groups are specified in your testng.xml file and can be found either under the <test> or <suite> tag.

@Test (groups = { "integrationTest", "functionalTest" , "nonfunctionalTest"})
public void logout(){
System.out.println("Logged out successfully");

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

<groups>
<define name="all">
<include name="integrationTest"/>
<include name="functionalTest"/>
<include name="nonfunctionalTest"/>
</define>
<run>
<include name="all" />
</run> 
</groups>

Question 11: How to run test cases in parallel using TestNG?

We can use “parallel” attribute in testng.xml in order to run test cases in parallel using TestNG.

<suite name="automationlaboratories" parallel="methods">

Question 12: How to exclude a particular test method from a test case execution?

By adding the exclude tag in the testng.xml test case can be skipped or excluded

<classes>
<class name="TestCaseName">
<methods>
<exclude name="TestMethodNameToExclude"/>
</methods>
</class> 
</classes>

Question 13: How to disable or Ignore Test case in TestNG?

Test case can be disabled by making enable = false

@Test(enabled = false)

Question 14: How state dependencies can be acheived in TestNG?

Dependencies can be declared in two different ways in TestNG

Using attributes dependsOnMethods in @Test annotations

public class DependsOnMethodsTestCase {

@Test(dependsOnMethods = {"testCase2"})
public void testCase1(){
System.out.println("Test Case 1");
}
@Test
public void testCase2(){
System.out.println("Test Case 2");
}

Using attributes dependsOnGroups in @Test annotations

public class DependsOnMethodsTestCase {
@Test(groups = {"FirstGroup"})
public void testCase1(){
System.out.println("Test Case 1");
}
@Test(groups = {"SecondGroup"})
public void testCase2(){
System.out.println("Test Case 2");
}
}

testng.xml file explained:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="softwaretestingmaterial">
<test name="testngTest">
<groups>
                    <dependencies>
                          <group name="FirstGroup" depends-on="SecondGroup"></group>
                    </dependencies>
                </groups>
<classes>
<class name="softwareTestingMaterial.DependsOnMethodsTestCase" />
</classes>
</test>
</suite>

Question 15: 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.

Question 16: How to run a test Method "n" number of times in  TestNG?

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

@Test(invocationCount = 10)
public void testCase1(){
System.out.println("Test Case 1");
}

Test Case 1 will be executed 10 number of times.

Question 17: How to create data driven framework using TestNG?

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

A test method that uses DataProvider will be executed the specific methods multiple number of times based on the data provided by the DataProvider. The test method will be executed using the same instance of the test class to which the test method belongs.

@DataProvider(name="getData")
public Object[][] getData(){
//Object [][] data = new Object [rowCount][colCount];
Object [][] data = new Object [2][2];
data [0][0] = "FirstUid";
data [0][1] = "FirstPWD";
data[1][0] = "SecondUid";
data[1][1] = "SecondPWD";
return data;
}

Question 18: What is @Factory Annotation in TestNG?

A factory will execute all the test methods present inside a test class using a separate instance of the respective class with different set of data.

Question 19: What is Thread Pool size and timeout in TestNG?

Thread Pool Size: is an attribute tells to form a thread pool to run the test method through multiple threads. Note: This attribute is ignored if invocationCount is not specified

Timeout: The maximum number of milliseconds a test case should take.

@Test(threadPoolSize = 3, invocationCount = 10, timeOut = 10000)
public void testCase1(){
System.out.println("Test Case 1");
}

Question 20: How to produce reports for TestNG results?

Reports can be produced in 2 ways mentioned below:

  1. Listeners implement the interface org.testng.ITestListener and are notified in real time of when a test starts, passes, fails, etc.
  2. 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.

BEST BOOKS AVAILABLE

q? encoding=UTF8&MarketPlace=IN&ASIN=1784394351&ServiceVersion=20070822&ID=AsinImage&WS=1&Format= SL250 &tag=7033b 21 - September 29, 2023 Automationlaboratoriesir?t=7033b 21&l=am2&o=31&a=1784394351 - September 29, 2023 Automationlaboratories

q? encoding=UTF8&MarketPlace=IN&ASIN=1788473574&ServiceVersion=20070822&ID=AsinImage&WS=1&Format= SL250 &tag=7033b 21 - September 29, 2023 Automationlaboratoriesir?t=7033b 21&l=am2&o=31&a=1788473574 - September 29, 2023 Automationlaboratories

q? encoding=UTF8&MarketPlace=IN&ASIN=1788999762&ServiceVersion=20070822&ID=AsinImage&WS=1&Format= SL250 &tag=7033b 21 - September 29, 2023 Automationlaboratoriesir?t=7033b 21&l=am2&o=31&a=1788999762 - September 29, 2023 Automationlaboratories

q? encoding=UTF8&MarketPlace=IN&ASIN=1788299671&ServiceVersion=20070822&ID=AsinImage&WS=1&Format= SL250 &tag=7033b 21 - September 29, 2023 Automationlaboratoriesir?t=7033b 21&l=am2&o=31&a=1788299671 - September 29, 2023 Automationlaboratories

q? encoding=UTF8&MarketPlace=IN&ASIN=B01EW3N03O&ServiceVersion=20070822&ID=AsinImage&WS=1&Format= SL250 &tag=7033b 21 - September 29, 2023 Automationlaboratoriesir?t=7033b 21&l=am2&o=31&a=B01EW3N03O - September 29, 2023 Automationlaboratories